我正在尝试将包含 YouTube 播放器的 View 添加到在我旋转设备时继续播放的 Activity 。由于 UI 包含的不仅仅是视频,我使用的是 YouTubePlayerFragment
。
当方向从纵向变为横向时,系统应该使用不同的布局文件。此布局还包括 YouTube 播放器作为不占据整个屏幕的 View 。您将在下面找到重现问题的最低限度代码(对于新的 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? 中接受的答案.不幸的是,这对我不起作用,因为纵向 View 和横向 View 的布局之间存在太多复杂的差异。
文档暗示了一种解决此问题的方法,使得回调方法 onInitializationSuccess
中的 boolean wasRestored
为 true
,但这是实际情况问题:上面代码中的wasRestored
总是 false
。
底线:如何让 YouTube 播放器在方向改变时继续播放,同时保持每个方向的单独布局(这是与链接问题的主要区别)?
最佳答案
onInitializationSuccess
返回一个 YoutubePlayer
对象,该对象在 getCurrentTimeMillis()
上有一个函数。将此对象保存为 onInitializationSuccess
内的 Activity 实例 (this.player = youTubePlayer
)。在 Activity 被销毁之前(oPause/onStop
),获取当前时间并将 is 传递给要存储的保留 fragment 。
关于android - 旋转具有不同方向布局的设备时继续播放 YouTube 播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44811557/