YouTubePlayerSupportFragment不播放视频

时间:2017-04-21 15:24:06

标签: java android video youtube android-youtube-api

我有一个包含两个片段的活动(就像youtube app一样。)

  1. YouTubePlayerSupportFragment - 播放视频的半宽。
  2. ListFragment包含像youtube一样的视频标题列表。
  3. 一旦活动启动,我就会在列表中播放第一个项目并播放视频,然后才有效。

    当从列表中选择任何视频时,我正在启动相同的活动(新实例),它可以很好地播放视频。 但是当我点击后退按钮并返回上一屏幕时。以前的视频没有播放它无限显示进度条

    在新实例中,我将在onDestroy上发布youtube播放器。

    和我正在尝试的旧实例仍然无用。

    @Override protected void onStart() {
        super.onStart();
        if (mYouTubePlayer != null) {
          Timber.i("onStart " + mYoutubeUrlId + " time " + mYouTubePlayer.getCurrentTimeMillis());
          mYouTubePlayer.loadVideo(mYoutubeUrlId, mYouTubePlayer.getCurrentTimeMillis());
        } else {
          Timber.i("onStart else " + mYoutubeUrlId);
        }
      }
    

    是否因为在多个Activity实例中使用Youtube片段?有什么建议摆脱问题吗?

    public class VideoDetailActivity extends BaseActivity
        implements YouTubePlayer.OnInitializedListener, YouTubePlayer.OnFullscreenListener,
        VideoDetailMVPView, TagListCallBack,
        SectionVideoListFragment.OnListFragmentInteractionListener {
      private static final int RECOVERY_DIALOG_REQUEST = 1;
    
      @Inject VideoDetailPresenter mPresenter;
      YouTubePlayer mYouTubePlayer;
      boolean isInFullscreen;
      String mYoutubeUrlId;
      @BindView(R.id.text_video_detail_title) TextView mVideoTitleView;
      @BindView(R.id.list_video_detail_tags) RecyclerView mVideoTagListView;
      @BindView(R.id.container_video_details) LinearLayout mVideoDetailContainer;
      TagDataAdapter mTagListAdapter;
    
      @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityComponent().inject(this);
        setContentView(R.layout.activity_video_detail);
        ButterKnife.bind(this);
        mPresenter.attachView(this);
        initialize();
      }
    
      void initialize() {
        setupToolbar();
        YouTubePlayerSupportFragment youtubeFragment =
            (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(
                R.id.video_container);
        youtubeFragment.setRetainInstance(true);
    
        youtubeFragment.initialize(AppConstants.YOUTUBE_API_KEY, this);
    
        SectionVideoListFragment mVideoListContainer =
            (SectionVideoListFragment) getSupportFragmentManager().findFragmentById(
                R.id.video_list_container);
    
        if (mVideoListContainer != null) {
          mVideoListContainer.setParametersForSection(1,
              AppConstants.DEFAULT_SECTION_ID);//FIXME: Need to changed.
    
          mTagListAdapter = new TagDataAdapter(this, null, this);
          mVideoTagListView.setHasFixedSize(true);
          mVideoTagListView.setLayoutManager(
              new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
          mVideoTagListView.setAdapter(mTagListAdapter);
        }
    
        getVideoById(getIntent().getStringExtra(AppConstants.EXTRA_VIDEO_ID));
      }
    
      void getVideoById(String videoId) {
        if (mPresenter != null && isNetworkConnected()) {
          mPresenter.getVideoByTitleId(videoId);
        }
      }
    
      @Override public boolean providesActivityToolbar() {
        return true;
      }
    
      @Override protected void onStop() {
        super.onStop();
        if (mYouTubePlayer != null && mYouTubePlayer.isPlaying()) {
          Timber.i(" Youtube -- onStop Activity and video paused " + mYoutubeUrlId);
          mYouTubePlayer.pause();
        } else {
          Timber.i(" Youtube -- onStop Activity and video not paused " + mYoutubeUrlId);
        }
      }
    
      private void setupToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        if (getSupportActionBar() != null) {
          //getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          getSupportActionBar().setTitle(R.string.title_activity_video_detail);
        }
      }
    
      @Override public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) finish();
        return super.onOptionsItemSelected(item);
      }
    
      @Override public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Timber.i("onConfigurationChanged : " + newConfig.orientation);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE && mYouTubePlayer != null) {
          mYouTubePlayer.setFullscreen(true);
        }
      }
    
      public static void launchScreen(Activity context, String videoId) {
        Intent intent = new Intent(context, VideoDetailActivity.class);
        intent.putExtra(AppConstants.EXTRA_VIDEO_ID, videoId);
        context.startActivity(intent);
      }
    
      @Override
      public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer,
          boolean wasRestored) {
        Timber.i("onInitializationSuccess" + mYoutubeUrlId);
        mYouTubePlayer = youTubePlayer;
        mYouTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
        if (!wasRestored && mYoutubeUrlId != null) mYouTubePlayer.loadVideo(mYoutubeUrlId);//AgzAbxRvBfs
        mYouTubePlayer.setOnFullscreenListener(this);
        mYouTubePlayer.setPlayerStateChangeListener(new PlayerStateChangeListener());
        mYouTubePlayer.setPlaybackEventListener(new PlaybackEventListener());
      }
    
      @Override public void onInitializationFailure(YouTubePlayer.Provider provider,
          YouTubeInitializationResult result) {
        if (result.isUserRecoverableError()) {
          result.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        } else {
          String errorMessage = String.format(getString(R.string.error_player), result.toString());
          Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
        }
      }
    
      @Override public void onFullscreen(boolean isInFullscreen) {
        this.isInFullscreen = isInFullscreen;
      }
    
      @Override protected void onStart() {
        super.onStart();
        if (mYouTubePlayer != null) {
          Timber.i("onStart " + mYoutubeUrlId + " time " + mYouTubePlayer.getCurrentTimeMillis());
          mYouTubePlayer.loadVideo(mYoutubeUrlId, mYouTubePlayer.getCurrentTimeMillis());
        } else {
          Timber.i("onStart else " + mYoutubeUrlId);
        }
      }
    
      @Override public void onGetVideoByIdSuccess(VideoDetails response) {
        if (mYouTubePlayer != null && !mYouTubePlayer.isPlaying()) {
          mYoutubeUrlId = response.getYoutubeId();
          mYouTubePlayer.loadVideo(mYoutubeUrlId);
        }
    
        mVideoDetailContainer.setVisibility(View.VISIBLE);
        mVideoTitleView.setText(response.getTitle());
        mTagListAdapter.updateData(response.getTagList());
      }
    
      @Override public void onError(Throwable exception) {
        onAPICallError(exception);
      }
    
      @Override public void onAPICallStarted(int messageId) {
    
      }
    
      @Override protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.detachView();
        if (mYouTubePlayer != null) {
          Timber.i(" Youtube -- destroy Activity and player released " + mYoutubeUrlId);
          mYouTubePlayer.release();
        } else {
          Timber.i(" Youtube -- destroy Activity and player not released " + mYoutubeUrlId);
        }
      }
    
      @Override public void onTagSelected(TagDetails tagDetails) {
        SectionVideoListActivity.launchScreenForTag(this, 1, tagDetails.getTagIdAsString(),
            tagDetails.getTagName());
      }
    
      @Override public void onItemClick(VideoDetails item) {
        VideoDetailActivity.launchScreen(this, item.getIdAsString());
        finish();
      }
    
      class PlaybackEventListener implements YouTubePlayer.PlaybackEventListener {
    
        @Override public void onPlaying() {
          Timber.i(" Youtube -- onPlaying " + mYoutubeUrlId);
        }
    
        @Override public void onPaused() {
          Timber.i(" Youtube -- onPaused " + mYoutubeUrlId);
        }
    
        @Override public void onStopped() {
          Timber.i(" Youtube -- onStopped " + mYoutubeUrlId);
        }
    
        @Override public void onBuffering(boolean b) {
          Timber.i(" Youtube -- onBuffering " + b + mYoutubeUrlId);
        }
    
        @Override public void onSeekTo(int i) {
          Timber.i(" Youtube -- onSeekTo " + mYoutubeUrlId);
        }
      }
    
      class PlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener {
    
        @Override public void onLoading() {
          Timber.i(" Youtube -- OnLoaded " + mYoutubeUrlId);
        }
    
        @Override public void onLoaded(String s) {
          Timber.i(" Youtube -- onLoaded " + mYoutubeUrlId);
        }
    
        @Override public void onAdStarted() {
          Timber.i(" Youtube -- onAdStarted" + mYoutubeUrlId);
        }
    
        @Override public void onVideoStarted() {
          Timber.i(" Youtube -- onVideoStarted " + mYoutubeUrlId);
        }
    
        @Override public void onVideoEnded() {
          Timber.i(" Youtube -- onVideoEnded " + mYoutubeUrlId);
        }
    
        @Override public void onError(YouTubePlayer.ErrorReason errorReason) {
          Timber.i(" Youtube -- onError " + errorReason.name() + mYoutubeUrlId);
        }
      }
    }
    

1 个答案:

答案 0 :(得分:0)

修复我的应用中的内存泄漏后,该错误不再出现。

因此,可以肯定地说,在播放视频时,Youtube SDK会检查是否有足够的内存。如果没有内存,通常会显示以下一般错误:

There was a problem while playing (Playback ID: ....) Tap to retry