我正在尝试将包含YouTube播放器的视图添加到我在旋转设备时继续播放的活动.由于UI不仅包含视频,因此我使用的是YouTubePlayerFragment.
当方向从纵向更改为横向时,系统应使用不同的布局文件.此布局还包括YouTube播放器作为不占用整个屏幕的视图.您将在下面找到重现问题的最低限度代码(对于全新的Android应用,最低API级别为19).
package com.example.youtubefragmenttest; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.google.android.youtube.player.YouTubeInitializationResult; import com.google.android.youtube.player.YouTubePlayer; import com.google.android.youtube.player.YouTubePlayerFragment; public class MainActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener { private static final String YOUTUBE_DEV_KEY = "[your youtube dev key here]"; private static final String TAG_YOUTUBE_FRAGMENT = "YoutubePlayerFragment"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm = getFragmentManager(); YouTubePlayerFragment retainedYoutubeFragment = (YouTubePlayerFragment) fm.findFragmentByTag(TAG_YOUTUBE_FRAGMENT); if (retainedYoutubeFragment == null) { retainedYoutubeFragment = YouTubePlayerFragment.newInstance(); retainedYoutubeFragment.setRetainInstance(true); FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragmentTransaction.add(R.id.youtube_fragment,retainedYoutubeFragment,TAG_YOUTUBE_FRAGMENT); fragmentTransaction.commit(); } retainedYoutubeFragment.initialize(YOUTUBE_DEV_KEY,this); } @Override public void onInitializationSuccess(YouTubePlayer.Provider provider,YouTubePlayer youTubePlayer,boolean wasRestored) { if (!wasRestored) { youTubePlayer.cueVideo("um4TrbU2Eic"); } } @Override public void onInitializationFailure(YouTubePlayer.Provider provider,YouTubeInitializationResult youTubeInitializationResult) { } }
这是布局文件:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.youtubefragmenttest.MainActivity"> <FrameLayout android:id="@+id/youtube_fragment" android:layout_width="200dp" android:layout_height="200dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" /> </android.support.constraint.ConstraintLayout>
我知道YouTubePlayer API的开发人员建议手动处理方向更改.这也是How to retain instance of fragment of fragment playing video after change in orientation?中接受的答案.不幸的是,这对我不起作用,因为纵向视图和横向视图的布局之间存在太多复杂的差异.
该文档暗示了一种修复此问题的方法,使得回调方法onInitializationSuccess中的boolean wasRestored为true,但这是实际问题:上面代码中的wasRestored始终为false.
底线:如何让YouTube播放器继续播放方向更改,同时保持每个方向的单独布局(这是与链接问题的主要区别)?
解决方法
onInitializationSuccess返回一个YoutubePlayer对象,该对象在getCurrentTimeMillis()上有一个函数.将此对象保存为onInitializationSuccess中的活动实例(this.player = youTubePlayer).在活动被销毁之前(oPause / onStop),获取当前时间并传递给要存储的保留片段.