Android YouTube api v3 - 方向 [英] Android YouTube api v3 - orientation

查看:23
本文介绍了Android YouTube api v3 - 方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Getting a sample activity up and running using YouTubeBaseActivity, YouTubePlayerView and YouTubePlayer was simple enough. I'm having trouble with orientation though and can't find any documentation or sample code. When I switch while a video is playing the screen is blank.

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

What are the correct things to do in each of onCreate(), onPause(), onSaveInstanceState() and onRestoreInstanceState() to have the video continue playing?

谢谢

推荐答案

变量

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

OnCreate

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

实现 OnInitializedListener

Implement 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

Inplement 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 的示例活动.

I've made sample activity which uses most recent youtube api.

此源处理方向问题"、媒体音量问题"、Youtube URL 解析问题"

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

  1. Here is git project for sample app

https://github.com/TheFinestArtist/YouTubePlayerActivity

我还制作了可以下载的示例应用

I also made sample app you can download

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

这篇关于Android YouTube api v3 - 方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆