android - 我对 YouTubePlayer 在 Android 中的可见性有一些疑问?

标签 android youtube youtube-api android-youtube-api

我对 android 中的 YouTube 播放器 View 有疑问。

当它播放一段时间后突然停止时,它会显示以下错误。 并显示以下错误。

YouTube video playback stopped because the player's view is not visible.
The view com.android.internal.policy.impl.PhoneWindow$DecorView
{36d050c2 I.E..... R.....ID 0,0-540,960} has visibility "INVISIBLE".

告诉我我能为这个错误做些什么。??

我的 Activity 代码在这里。

youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_player);
youTubePlayerView.initialize(GOOGLE_API_KEY, this);


@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
        YouTubePlayer youTubePlayer, boolean wasRestored) {
    myYouTubePlayer = youTubePlayer;
    // Change play style
    youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
    youTubePlayer
            .addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
    youTubePlayer.setPlayerStateChangeListener(this);
    youTubePlayer.setPlaybackEventListener(this);
    youTubePlayer.loadVideo("wKJ9KzGQq0w");
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
        YouTubeInitializationResult errorReason) {
    if (errorReason.isUserRecoverableError()) {
        errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
    } else {
        String errorMessage = String.format(
                "There was an error initializing the YouTubePlayer (%1$s)",
                errorReason.toString());
        Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
    }
}

我的 XML 文件代码在这里。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_FullScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" >

<com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/youtube_player"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/rl_CurrentVideoDetails"
    android:layout_below="@+id/rl_Heading"
    android:layout_toStartOf="@+id/rl_ChannelChange"
    android:background="#000" />

<RelativeLayout
    android:id="@+id/rl_Heading"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="12dp"
        android:contentDescription="@null"
        android:padding="8dp" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/rl_ChannelChange"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    android:layout_alignParentEnd="true"
    android:background="@color/transparent_background_black"
    android:gravity="center_horizontal" >

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_above="@+id/ll_ChNo"
        android:layout_marginBottom="16dp"
        android:contentDescription="@null"
        android:padding="8dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@null"
            android:gravity="center"
            android:padding="4dp"
            android:text="Ch."
            android:textSize="16sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@null"
            android:gravity="center"
            android:padding="4dp"
            android:text="123"
            android:textSize="16sp" />
    </LinearLayout>

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_below="@+id/ll_ChNo"
        android:layout_marginTop="16dp"
        android:contentDescription="@null"
        android:padding="8dp"/>
</RelativeLayout>

<RelativeLayout
    android:id="@+id/rl_CurrentVideoDetails"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerVertical="true"
    android:background="@color/transparent_background_black"
    android:paddingStart="12dp" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="6dp"
        android:singleLine="true"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_CurrentVideoName"
        android:orientation="horizontal"
        android:padding="6dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:textAllCaps="true"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="| Ch. "
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="4dp"
            android:singleLine="true"
            android:textAllCaps="true" />
    </LinearLayout>

    <ImageView
        android:id="@+id/iv_NormalScreen"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:contentDescription="@null"
        android:padding="10dp" />
</RelativeLayout>

最佳答案

昨晚我在我的应用程序上遇到了同样的问题,并最终找到了解决方案(至少对我而言)。我的应用程序似乎一直存在问题,我不小心多次向我的 YoutubePlayerActivity 启动相同的 Intent 。

以前,我自己没有注意到这一点 - 但是最近的 Youtube 更新一定增加了限制或改变了它的工作方式,然后我收到了这个错误。

我检查了我的代码,发现当我添加 fragment 时,它会重新调用每个 fragment 的 onCreateView。对于所有添加的三个 fragment 的含义,我最终调用了三次 Intent 。

希望这对您有所帮助。

关于android - 我对 YouTubePlayer 在 Android 中的可见性有一些疑问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32821018/

相关文章:

android - 获取包含参数的 YouTube 视频 ID

php - 如何在 PHP 中通过 HTTPS 发出 POST 请求?

c# - YouTube上传API-离线获取Auth token

java - setOnItemClickListener 不起作用

java - Delphi 使用 Android Jni 包装器中的过程

wordpress - 在div中插入自动嵌入的Wordpress YouTube

php - 如何使用v3发送消息/视频推荐?

java - 将多媒体与 LibGDX 集成

android - 使用适配器模式实现 ChipGroup

youtube - 可点击的预览图片,可自动加载YouTube视频