YouTubePlayerSupportFragment not playing Video

倾然丶 夕夏残阳落幕 提交于 2020-05-13 17:51:31

问题


I Have an Activity having two fragments (Just like youtube app.)

  1. YouTubePlayerSupportFragment - half width which plays video.
  2. ListFragment contains list of video title list like youtube.

As soon as activity launches i am taking first item in list and playing video and it works.

When any video is selected from list, I am launching same activity(new instance) it plays video well. But when i click back button and come back to previous screen. previous video is not playing it is showing progressbar infinitely.

In new Instance I am releasing youtube player on onDestroy.

and old instance i am trying this still not useful.

@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);
    }
  }

Is it due to using Youtube fragment in multiple instance of Activity? Any suggestions to get rid of problem?

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);
    }
  }
}

来源:https://stackoverflow.com/questions/43546504/youtubeplayersupportfragment-not-playing-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!