10

使用 YouTubeBaseActivity、YouTubePlayerView 和 YouTubePlayer 启动并运行示例活动非常简单。我在定位方面遇到了问题,找不到任何文档或示例代码。当我在播放视频时切换屏幕是空白的。

在 onCreate()、onPause()、onSaveInstanceState() 和 onRestoreInstanceState() 中,为了让视频继续播放,正确的做法是什么?

谢谢

4

2 回答 2

27

变量

@SuppressLint("InlinedApi")
private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9
        ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;

@SuppressLint("InlinedApi")
private static final int LANDSCAPE_ORIENTATION = Build.VERSION.SDK_INT < 9
        ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        : ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;

private YouTubePlayer mPlayer = null;
private boolean mAutoRotation = false;

创建时

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAutoRotation = Settings.System.getInt(getContentResolver(),
            Settings.System.ACCELEROMETER_ROTATION, 0) == 1;
}

实现 OnInitializedListener

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
        boolean wasRestored) {
    mPlayer = player;
    player.setOnFullscreenListener(this);
    
    if (mAutoRotation) {
        player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
                | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
                | YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE
                | YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
    } else {
        player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
                | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
                | YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
    }
}

实现 onConfigurationChanged

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        if (mPlayer != null)
            mPlayer.setFullscreen(true);
    } 
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        if (mPlayer != null)
            mPlayer.setFullscreen(false);
    }
}

@Override
public void onFullscreen(boolean fullsize) {
    if (fullsize) {
        setRequestedOrientation(LANDSCAPE_ORIENTATION);
    } else {
        setRequestedOrientation(PORTRAIT_ORIENTATION);
    }
}

清单

 <activity
    android:name="com.sample.android.YouTubePlayerActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:screenOrientation="sensor"
    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
</activity>

我制作了使用最新 youtube api 的示例活动。

此源处理“方向问题”、“媒体量问题”、“ Youtube 网址解析问题

  1. 这是示例应用程序的 git 项目

    https://github.com/TheFinestArtist/YouTubePlayerActivity

  2. 我还制作了示例应用程序,您可以下载

    https://play.google.com/store/apps/details?id=com.thefinestartist.ytpa.sample

于 2013-05-09T05:09:57.860 回答
1

把这个留给其他人。最好的艺术家的回答也对我有用,但由于我在片段中使用视频播放器并且还必须支持 AppCompatLibrary,这就是我能够得到的工作:

YouTubePlayerActivityFragment.java

public class YouTubePlayerActivityFragment extends YouTubePlayerSupportFragment implements
    YouTubePlayer.OnFullscreenListener {

@SuppressLint("InlinedApi")
private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9
        ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;

@SuppressLint("InlinedApi")
private static final int LANDSCAPE_ORIENTATION = Build.VERSION.SDK_INT < 9
        ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        : ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;

private YouTubePlayer.OnFullscreenListener fullScreenListener = null;
private YouTubePlayer yPlayer = null;
private boolean mAutoRotation = false;


public static YouTubePlayerActivityFragment newInstance(String videoID, String apiKey) {
    YouTubePlayerActivityFragment fragment = new YouTubePlayerActivityFragment();
    Bundle bundle = new Bundle();
    bundle.putString("video_id", videoID);
    bundle.putString("api_key", apiKey);
    fragment.setArguments(bundle);
    fragment.init();
    return fragment;
}

public YouTubePlayerActivityFragment() {

}

@Override
public void onCreate(Bundle var1) {
    super.onCreate(var1);
    mAutoRotation = Settings.System.getInt(getActivity().getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1;
    fullScreenListener = this;
}

private void init() {
    initialize(getArguments().getString("api_key"), new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
            yPlayer = youTubePlayer;
            youTubePlayer.setOnFullscreenListener(fullScreenListener);
            if (mAutoRotation) {
                youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
                        | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
                        | YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE
                        | YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
            } else {
                youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION
                        | YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI
                        | YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
            }

            if (!wasRestored) {
                yPlayer.loadVideo(getArguments().getString("video_id"));
            }
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

        }
    });
}

@Override
public void onFullscreen(boolean isFullSize) {
    if (isFullSize) {
        getActivity().setRequestedOrientation(LANDSCAPE_ORIENTATION);
    } else {
        getActivity().setRequestedOrientation(PORTRAIT_ORIENTATION);
    }
}
于 2015-08-03T14:03:14.437 回答