当前位置: 首页 > 知识库问答 >
问题:

Android YouTube API-在没有缓冲的情况下处理方向更改

沃驰
2023-03-14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/youtube_ll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_shape"
    android:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@drawable/header_background"
        android:weightSum="2" >

        <TextView
            android:id="@+id/youtube_username_tv"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="top|left"
            android:paddingLeft="15dp"
            android:paddingTop="8dp"
            android:paddingBottom="5dp"
            android:textColor="@android:color/darker_gray"
            android:textSize="13sp" />

        <TextView
            android:id="@+id/youtube_when_tv"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="top|right"
            android:paddingRight="15dp"
            android:paddingTop="8dp"
            android:paddingBottom="5dp"
            android:textColor="@android:color/darker_gray"
            android:textSize="13sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/youtube_content_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingRight="17dp"
        android:paddingTop="8dp"
        android:paddingBottom="15dp"
        android:textSize="15sp" />

</LinearLayout>
public class YouTubeParser extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener, YouTubePlayer.PlayerStateChangeListener, YouTubePlayer.OnFullscreenListener {

    private static final int RECOVERY_DIALOG_REQUEST = 1;
    private String youtubeUrl;
    private com.google.android.youtube.player.YouTubePlayerView youtube;
    private YouTubePlayer player;
    private Context mContext;
    private boolean fullscreen;

    private static final int PORTRAIT_ORIENTATION = Build.VERSION.SDK_INT < 9
            ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                    : ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;

    public YouTubeParser(View v, Context context, Post post, String lastUrl) {
        this.mContext = context;
        youtubeUrl = post.getYoutubeUrl();
        TextView username = (TextView) v.findViewById(R.id.youtube_username_tv);
        TextView when = (TextView) v.findViewById(R.id.youtube_when_tv);
        TextView content = (TextView) v.findViewById(R.id.youtube_content_tv);


        if (username != null)
            username.setText(post.getUsername());
        if (when != null)
            when.setText(post.getWhen());
        if (content != null) {
            content.setText(Html.fromHtml(post.getContent()));
            content.setMovementMethod(LinkMovementMethod.getInstance());
        }
        if (lastUrl != youtubeUrl) {
            youtube = new com.google.android.youtube.player.YouTubePlayerView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) LayoutParams.MATCH_PARENT, (int) LayoutParams.WRAP_CONTENT);
            youtube.setLayoutParams(params);

            LinearLayout ll = (LinearLayout) v.findViewById(R.id.youtube_ll);
            if (ll.getChildCount() == 4)
                ll.removeViewAt(2);
            ll.addView(youtube, 2);
        }
        if (youtube != null) {
            youtube.initialize(DeveloperKey.DEVELOPER_KEY, this);
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format(getString(0), errorReason.toString());
            Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            // Retry initialization if user performed a recovery action
            getYouTubePlayerProvider().initialize(DeveloperKey.DEVELOPER_KEY, this);
        }
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
        this.player = player;
        player.setPlayerStateChangeListener(this);
        if (!wasRestored) {
            player.cueVideo(youtubeUrl);
            Log.i("Position", "video cued: " + youtubeUrl);
        }
    }

    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return youtube;
    }

    public void setNoLandscape() {
        if (player != null) {
            int controlFlags = player.getFullscreenControlFlags();
            controlFlags &= ~YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
            player.setFullscreenControlFlags(controlFlags);
            if (mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
                player.pause();
        }
    }

    public void setToLandscape() {
        if (player != null) {
            int controlFlags = player.getFullscreenControlFlags();
            controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
            player.setFullscreenControlFlags(controlFlags);
        }
    }

    @Override
    public void onAdStarted() {
    }

    @Override
    public void onError(ErrorReason arg0) {
    }

    @Override
    public void onLoaded(String arg0) {
    }

    @Override
    public void onLoading() {
    }

    @Override
    public void onVideoEnded() {
        setNoLandscape();
    }

    @Override
    public void onVideoStarted() {
        int controlFlags = player.getFullscreenControlFlags();
        controlFlags |= YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE;
        player.setFullscreenControlFlags(controlFlags);
    }

    @Override
    public void onFullscreen(boolean isFullscreen) {
        fullscreen = isFullscreen;
        doLayout();

    }

    private void doLayout() {
        LinearLayout.LayoutParams playerParams =
                (LinearLayout.LayoutParams) youtube.getLayoutParams();
        if (fullscreen) {
            // When in fullscreen, the visibility of all other views than the player should be set to
            // GONE and the player should be laid out across the whole screen.
            playerParams.width = LayoutParams.MATCH_PARENT;
            playerParams.height = LayoutParams.MATCH_PARENT;

        } else {
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                playerParams.width = 0;
                playerParams.height = WRAP_CONTENT;
                playerParams.weight = 1;
            } else {
                playerParams.width = MATCH_PARENT;
                playerParams.height = WRAP_CONTENT;
                playerParams.weight = 0;
            }
        }
    }

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


}

共有1个答案

施越彬
2023-03-14

通过向android清单添加configChanges:

<activity android:name=".InfoActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/app_name" >
</activity>

简单地修复我的问题:

  • 再次开始缓冲
  • 从头再来
 类似资料:
  • 问题内容: 在Java中,我们使用try catch块处理异常。我知道我可以像下面这样编写一个try catch块来捕获方法中抛出的任何异常。 但是Java中有什么方法可以让我在发生异常时获取一种称为的特定方法,而不是像上面的方法那样编写一个包罗万象的方法? 具体来说,当抛出异常(我的应用程序逻辑未处理)时,我想在Swing应用程序中显示一条用户友好的消息。 谢谢。 问题答案: 默认情况下,JVM

  • 就我所知,没有简单的方法可以做到这一点:如果我理解正确的话,像这样的会因为类型别名而导致未定义的行为,如果不复制数据和分配额外的空间,就不可能使用或float/double,这违背了目的,而且在我的情况下代价很高(在C++中不允许使用union进行类型双关语)。 可以假设浮动缓冲区正确对齐,以便将其用于双打。

  • 我试图在不使用Box2D的情况下为libgdx角色(玩家和敌人)实现碰撞检测。正如我所读到的那样,Box2D支持内置碰撞检测,但由于我的游戏不涉及环境中的任何物理因素,因此我不习惯仅为此使用Box2D。 我发现的许多示例通过为此定义一个边界框(矩形)来启用冲突检测,但我正在寻找一个内置的解决方案。

  • 我正在尝试在没有项目编写器的情况下使用下面的配置来配置spring批处理步骤。然而,我错误地说writer元素既没有'writer'属性,也没有元素。 我浏览了链接spring批处理:没有ItemWriter的Tasklet。但无法解决问题。有人能告诉我在我提到的代码片段中要做的具体更改吗

  • 我刚刚意识到,在Lollipop下面的Android版本上,不可能通过appcompat改变状态栏的颜色。我目前正在运行Android Kitkat 4.4.2,并收到了一个应用程序的更新,以下是截图 我的问题是,他们是如何在运行比Lollipop低版本的设备上不使用appcompat而改变状态栏颜色的? 怎么可能改变颜色?

  • 文档说这个库运行在GPU上。如果我功能强大的笔记本电脑没有GPU,我还能运行Deeplearning4J吗?