【问题标题】:I have some issue with Visibility of YouTubePlayer in android?我对 Android 中 YouTubePlayer 的可见性有一些问题?
【发布时间】:2015-12-25 13:53:20
【问题描述】:

我对 android 中的 youtube 播放器视图有疑问。

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

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".

告诉我这个错误我该怎么办。??

我的活动代码在这里。

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>

【问题讨论】:

  • 尝试为 youtube 播放器的高度和宽度提供一些固定的特定值,看看它是否有效。
  • 不,它不工作仍然有问题。
  • 使用可以在他们的官方网站上找到的最新 youtube api,并在更新的 Youtube 应用上运行您的应用。
  • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post。
  • @GyanendraMani ...我这样做了,但它仍然不起作用兄弟

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


【解决方案1】:

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

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

我浏览了我的代码,发现当我添加片段时,它正在重新调用每个片段的 onCreateView。对于添加的所有三个片段的含义,我最终将意图调用了 3 次。

希望这会有所帮助。

【讨论】:

  • 所以你的意思是你必须检查我是否记得活动中的那个片段?
  • 不错的观察。它帮助很大。
【解决方案2】:

您能否检查一下您的 Youtube 应用程序是否在去年 9 月或之后更新? Youtube App 中似乎有一个错误,在使用 Youtube API 时会导致这个 estrange 错误:

https://code.google.com/p/gdata-issues/issues/detail?id=7586

您可以轻松检查此错误是否由 Youtube 应用程序引起,删除 Youtube 应用程序更新并测试您的应用程序。您可以前往“设置”>“通用”>“应用”>“Youtube”删除此应用更新,然后单击“卸载更新”按钮。

对我来说,这就是我的应用 youtube 视频无法正常运行的原因。当我卸载更新时,我的应用程序正常工作,当我重新安装更新时,我的应用程序再次崩溃。所以,我认为唯一的解决方案是等待新的更新:(

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    YouTube 视频播放停止,因为播放器的视图不在 可见。

    任何内容都不应与YouTubePlayerView 重叠(根据 Google 的政策要求)。这意味着您需要考虑层次结构中所有视图的 z-index 和/或重叠。

    在您的情况下,XML 文件中元素的顺序决定了 z-index,并且有一个与 YouTubePlayerView 重叠的 match_parent 布局:您需要将YouTubePlayerView 放在前面,只需将其作为布局中的最后一项移动即可:

    <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>
    
    <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" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 2011-02-21
      • 2011-01-31
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多