代码之家  ›  专栏  ›  技术社区  ›  ironmantis7x

YouTube播放器活动出错

  •  0
  • ironmantis7x  · 技术社区  · 7 年前

    好的,我在我的项目中使用的是Android Studio 3.0和YouTube API版本3。 我已经将库/jar文件添加到项目中(通过这个链接)。 项目已编译。

    有一件奇怪的事我想不出来:当我运行应用程序并选择一个视频时,应用程序说“初始化YouTube播放器时出错”,这发生在设备和模拟器上。

    我查看了IDE,查看了logcat,我发现IDE上有一条错误消息:

    IDE error of YouTube Activity content

    error for YouTube modue

    我做错了什么?我该如何修复它,以便在选中YouTube视频时播放它们?

    我在这里添加了我的YouTube活动文件代码:

    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Toast;
    import android.widget.VideoView;
    
    import com.google.android.youtube.player.YouTubeBaseActivity;
    import com.google.android.youtube.player.YouTubeInitializationResult;
    import com.google.android.youtube.player.YouTubePlayer;
    import com.google.android.youtube.player.YouTubePlayerView;
    
    
    public class YouTubePlaybackOverlayActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
    
        private static String ytUrl;
        public String TAG = YouTubePlaybackOverlayActivity.class.getSimpleName();
    
        public VideoView mVideoView;
    
        public LeanbackPlaybackState mPlaybackState = LeanbackPlaybackState.IDLE;
    
        public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXX";
    
        private int mPosition = 0;
        private long mStartTimeMillis;
        private long mDuration = -1;
        /**
         * ATTENTION: This was auto-generated to implement the App Indexing API.
         * See https://g.co/AppIndexing/AndroidStudio for more information.
         */
        //private GoogleApiClient client;
    
        @Override
        public void onStart() {
            super.onStart();
    
    //        // ATTENTION: This was auto-generated to implement the App Indexing API.
    //        // See https://g.co/AppIndexing/AndroidStudio for more information.
    //        client.connect();
    //        Action viewAction = Action.newAction(
    //                Action.TYPE_VIEW, // TODO: choose an action type.
    //                "PlaybackOverlay Page", // TODO: Define a title for the content shown.
    //                // TODO: If you have web page content that matches this app activity's content,
    //                // make sure this auto-generated web page URL is correct.
    //                // Otherwise, set the URL to null.
    //                Uri.parse("http://host/path"),
    //                // TODO: Make sure this auto-generated app deep link URI is correct.
    //                Uri.parse("android-app://software.blackstone.sunnahstreamtv/http/host/path")
    //        );
    //        AppIndex.AppIndexApi.start(client, viewAction);
        }
    
        @Override
        public void onStop() {
            super.onStop();
    
    //        // ATTENTION: This was auto-generated to implement the App Indexing API.
    //        // See https://g.co/AppIndexing/AndroidStudio for more information.
    //        Action viewAction = Action.newAction(
    //                Action.TYPE_VIEW, // TODO: choose an action type.
    //                "PlaybackOverlay Page", // TODO: Define a title for the content shown.
    //                // TODO: If you have web page content that matches this app activity's content,
    //                // make sure this auto-generated web page URL is correct.
    //                // Otherwise, set the URL to null.
    //                Uri.parse("http://host/path"),
    //                // TODO: Make sure this auto-generated app deep link URI is correct.
    //                Uri.parse("android-app://software.blackstone.sunnahstreamtv/http/host/path")
    //        );
    //        AppIndex.AppIndexApi.end(client, viewAction);
    //        client.disconnect();
        }
    
        /*
         * List of various states that we can be in
         */
        public enum LeanbackPlaybackState {
            PLAYING, PAUSED, IDLE
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            /** attaching layout xml **/
            //setContentView(R.layout.nabawi_video);
            //setContentView(R.layout.activity_playback_overlay);
            setContentView(R.layout.video_playback);
    
            /** Initializing YouTube player view **/
            YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
            youTubePlayerView.initialize(API_KEY, this);
            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            //client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
        }
    
    
    //    @Override
    //    protected void onCreate(Bundle savedInstanceState) {
    //        super.onCreate(savedInstanceState);
    //        setContentView(R.layout.activity_playback_overlay);
    //
    //        loadViews();
    //    }
    
        @Override
        public void onDestroy() {
            stopPlayback();
            super.onDestroy();
        }
    
        private void loadViews() {
            mVideoView = (VideoView) findViewById(R.id.youtube_player);
            mVideoView.setFocusable(false);
            mVideoView.setFocusableInTouchMode(false);
    
            Movie movie = (Movie) getIntent().getSerializableExtra(DetailsActivity.MOVIE);
            //setVideoPath(movie.getVideoUrl());
            setVideoPath(movie.getyTubeID());
            ytUrl = movie.getyTubeID();
        }
    
        public void setVideoPath(String videoUrl) {
            setPosition(0);
            mVideoView.setVideoPath(videoUrl);
            mStartTimeMillis = 0;
            mDuration = Utils.getDuration(videoUrl);
        }
    
        private void stopPlayback() {
            if (mVideoView != null) {
                mVideoView.stopPlayback();
            }
        }
    
        private void setPosition(int position) {
            if (position > mDuration) {
                mPosition = (int) mDuration;
            } else if (position < 0) {
                mPosition = 0;
                mStartTimeMillis = System.currentTimeMillis();
            } else {
                mPosition = position;
            }
            mStartTimeMillis = System.currentTimeMillis();
            Log.d(TAG, "position set to " + mPosition);
        }
    
        public int getPosition() {
            return mPosition;
        }
    
        public void setPlaybackState(LeanbackPlaybackState playbackState) {
            this.mPlaybackState = playbackState;
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_playback_overlay, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
        public void playPause(boolean doPlay) {
            if (mPlaybackState == LeanbackPlaybackState.IDLE) {
                /* Callbacks for mVideoView */
                setupCallbacks();
            }
    
            if (doPlay && mPlaybackState != LeanbackPlaybackState.PLAYING) {
                mPlaybackState = LeanbackPlaybackState.PLAYING;
                if (mPosition > 0) {
                    mVideoView.seekTo(mPosition);
                }
                mVideoView.start();
                mStartTimeMillis = System.currentTimeMillis();
            } else {
                mPlaybackState = LeanbackPlaybackState.PAUSED;
                int timeElapsedSinceStart = (int) (System.currentTimeMillis() - mStartTimeMillis);
                setPosition(mPosition + timeElapsedSinceStart);
                mVideoView.pause();
            }
        }
    
        public void fastForward() {
            if (mDuration != -1) {
                // Fast forward 10 seconds.
                setPosition(mVideoView.getCurrentPosition() + (10 * 1000));
                mVideoView.seekTo(mPosition);
            }
        }
    
        public void rewind() {
            // rewind 10 seconds
            setPosition(mVideoView.getCurrentPosition() - (10 * 1000));
            mVideoView.seekTo(mPosition);
        }
    
        private void setupCallbacks() {
    
            mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
    
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    mVideoView.stopPlayback();
                    mPlaybackState = LeanbackPlaybackState.IDLE;
                    return false;
                }
            });
    
            mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    if (mPlaybackState == LeanbackPlaybackState.PLAYING) {
                        mVideoView.start();
                    }
                }
            });
    
            mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mPlaybackState = LeanbackPlaybackState.IDLE;
                }
            });
        }
    
        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
            Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
        }
    
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
            /** add listeners to YouTubePlayer instance **/
            player.setPlayerStateChangeListener(playerStateChangeListener);
            player.setPlaybackEventListener(playbackEventListener);
            player.setFullscreen(true);
            player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
            //player.play();
    
    
            /** Start buffering **/
    
            for (int i = 0; i < MovieProvider.mItems.size(); i++) {
                Movie movie = (Movie) getIntent().getSerializableExtra(DetailsActivity.MOVIE);
                player.loadVideo(movie.getyTubeID());
            }
    
        }
    
        private YouTubePlayer.PlaybackEventListener playbackEventListener = new YouTubePlayer.PlaybackEventListener() {
    
            @Override
            public void onBuffering(boolean arg0) {
            }
    
            @Override
            public void onPaused() {
            }
    
            @Override
            public void onPlaying() {
            }
    
            @Override
            public void onSeekTo(int arg0) {
            }
    
            @Override
            public void onStopped() {
            }
    
        };
    
        private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener = new YouTubePlayer.PlayerStateChangeListener() {
    
            @Override
            public void onAdStarted() {
            }
    
            @Override
            public void onError(YouTubePlayer.ErrorReason arg0) {
            }
    
            @Override
            public void onLoaded(String arg0) {
            }
    
            @Override
            public void onLoading() {
            }
    
            @Override
            public void onVideoEnded() {
            }
    
            @Override
            public void onVideoStarted() {
            }
        };
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   ironmantis7x    7 年前

    该问题是该项目中的一个腐蚀性索引。 我删除了类文件。 然后我清除了缓存,重启了Android Studio,然后清理并重建了项目。 我重新启动了Android Studio,然后重新创建了所需的类文件和布局文件,项目编译成功,一切都很好。

    感谢大家的时间、建议、建议和投入。