旋转具有单独方向布局的设备时,继续播放YouTube播放器 [英] Continue playback of YouTube player while rotating device with separate orientation layouts

查看:90
本文介绍了旋转具有单独方向布局的设备时,继续播放YouTube播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将包含YouTube播放器的视图添加到一个活动中,该活动在旋转设备时会继续播放.由于UI不仅包含视频,因此我使用的是YouTubePlayerFragment.

I am trying to add a View containing a YouTube Player to an Activity that keeps on playing when I rotate the device. Since the UI contains more than just the video, I am using a YouTubePlayerFragment.

当方向从纵向更改为横向时,系统应使用其他布局文件.此布局还包括YouTube播放器,该视图不会占据整个屏幕.在下面,您将找到重现该问题的最低限度代码(对于全新的Android应用,最低API级别为19).

When the orientation changes from portrait to landscape, the system should use a different layout file. This layout also includes the YouTube player as a view which does not take up the whole screen. Below you will find the bare minimum code to reproduce the problem (for a fresh Android app, minimum API level 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的开发人员建议手动处理方向更改.这也是

I know the developers of the YouTubePlayer API recommend handling orientation changes manually. This is also the accepted answer in How to retain instance of fragment of fragment playing video after change in orientation?. Unfortunately, this will not work for me because there are too many complex differences between the layouts of portrait view and landscape view.

文档提示了一种解决方法,以使回调方法onInitializationSuccess中的boolean wasRestoredtrue,但这是实际的问题:上面代码中的wasRestored始终为 false.

The documentation hints at a way to fix this such that boolean wasRestored in the callback method onInitializationSuccess is true, but this is the actual problem: wasRestored in the code above is always false.

底线:如何保持YouTube播放器继续播放方向更改,同时保持每个方向的单独布局(这是链接问题的主要区别)?

Bottom line: how do you keep the YouTube player continue playing on orientation change while keeping separate layouts per orientation (which is the main difference with linked question)?

推荐答案

onInitializationSuccess返回在getCurrentTimeMillis()上具有功能的YoutubePlayer对象.将此对象另存为onInitializationSuccess中的活动实例(this.player = youTubePlayer).在销毁活动之前(oPause/onStop),获取当前时间,然后传递到要存储的保留片段.

onInitializationSuccess returns a YoutubePlayer object that has a function on getCurrentTimeMillis(). Save this object as an activity instance (this.player = youTubePlayer) inside onInitializationSuccess. Before the activity is destroyed(oPause/onStop), get the current time and pass is to the retained fragment to be stored.

这篇关于旋转具有单独方向布局的设备时,继续播放YouTube播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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