0

我正在为 youtube 视频进行直播。通过进入画中画模式,播放器会使用错误UNAUTHORIZED_OVERLAY暂停视频。

视频布局.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_youtube_rootlayout"
android:orientation="vertical"
android:background="@color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.youtube.player.YouTubePlayerView
    android:id="@+id/youtube_player"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

活动.cs

 [Activity(Label = "", ResizeableActivity = true, Theme = "@style/Theme.MyAppTheme", TaskAffinity = "com.m", MainLauncher =true,AllowTaskReparenting = true, AutoRemoveFromRecents = true, ExcludeFromRecents = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTask, SupportsPictureInPicture = true/*, ConfigurationChanges = Android.Content.PM.ConfigChanges.ScreenSize | Android.Content.PM.ConfigChanges.SmallestScreenSize | Android.Content.PM.ConfigChanges.ScreenLayout | Android.Content.PM.ConfigChanges.Orientation*/)]
public class YoutubeActivity: YouTubeBaseActivity,IYouTubePlayerOnInitializedListener,View.IOnClickListener,IYouTubePlayerPlayerStateChangeListener,IYouTubePlayerPlaybackEventListener,IYouTubePlayerOnFullscreenListener
{

    private YouTubePlayerView mYoutubePlayer;
    private PictureInPictureParams.Builder pictureInPictureParamsBuilder =
           new PictureInPictureParams.Builder();
    private LinearLayout linear_rootlayout;
    private TextView txtMinimizevideo,txtCloseVideo;
    private IYouTubePlayer youtubevideo;
    private bool  isbackbuttonpress = false;
    private RelativeLayout relative_youtubecontrols;

    public void OnInitializationFailure(IYouTubePlayerProvider p0, YouTubeInitializationResult p1)
    {

    }

    public void OnInitializationSuccess(IYouTubePlayerProvider provider, IYouTubePlayer player, bool p2)
    {
        this.youtubevideo = player;
        // youtubevideo.SetPlayerStyle(YouTubePlayerPlayerStyle.Minimal); 

        youtubevideo.SetOnFullscreenListener(this);
        youtubevideo.SetPlayerStateChangeListener(this);
        youtubevideo.SetPlaybackEventListener(this);

         youtubevideo.FullscreenControlFlags = YouTubePlayer.FullscreenFlagCustomLayout;

        youtubevideo.LoadVideo("VideoKey");

    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.youtube_player_layout);

        mYoutubePlayer = FindViewById<YouTubePlayerView>(Resource.Id.youtube_player);
        linear_rootlayout = FindViewById<LinearLayout>(Resource.Id.linear_youtube_rootlayout);
        relative_youtubecontrols = FindViewById<RelativeLayout>(Resource.Id.rel_youtube_control);
        mYoutubePlayer.Initialize("SerialKey", this);



    }


    public override void OnPictureInPictureModeChanged(bool isInPictureInPictureMode, Configuration newConfig)
    {
        base.OnPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
        if (IsInPictureInPictureMode)
        {

            youtubevideo.Release();
            Window.AddFlags(WindowManagerFlags.Fullscreen);

        }

    }

    public override void OnBackPressed()
    {
        pictureInPictureMode();
    }

    protected override void OnUserLeaveHint()
    {
        base.OnUserLeaveHint();
        if (!IsInPictureInPictureMode)
        {
            pictureInPictureMode();
        }

    }

    private void pictureInPictureMode()
    {
        isbackbuttonpress = true;
        Rational aspectRatio = new Rational(200, 110);
        pictureInPictureParamsBuilder.SetAspectRatio(aspectRatio).Build();
        EnterPictureInPictureMode(pictureInPictureParamsBuilder.Build());

    }


    public void OnBuffering(bool p0)
    {

    }

    public void OnPaused()
    {

    }

    public void OnPlaying()
    {

    }

    public void OnSeekTo(int p0)
    {

    }

    public void OnStopped()
    {
       // youtubevideo.Play();
    }

    public void OnAdStarted()
    {

    }

    public void OnError(YouTubePlayerErrorReason p0)
    {

    }

    public void OnLoaded(string p0)
    {
        youtubevideo.Play();
    }

    public void OnLoading()
    {

    }

    public void OnVideoEnded()
    {

    }

    public void OnVideoStarted()
    {

    }

    public void OnFullscreen(bool p0)
    {

    }
}

}

我在 oninitializedsuccess 上加载视频并在 onLoaded 中播放视频。我已经尝试了所有可能的解决方案,因此 youtube 播放器视图顶部没有视图,但它总是给我同样的错误。

4

1 回答 1

1

我认为问题在于您的OnPictureInPictureModeChanged方法..

public override void OnPictureInPictureModeChanged(bool isInPictureInPictureMode, Configuration newConfig)
{
    base.OnPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
    if (IsInPictureInPictureMode)
    {

        youtubevideo.Release();
        Window.AddFlags(WindowManagerFlags.Fullscreen);

    }

    }

我在这里看到错误UNAUTHORIZED_OVERLAY弹出的原因是您在 PIP 模式下调用 AddFlags ..

如下修改您的代码并检查错误是否仍然存在..

@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
if (isInPictureInPictureMode) {
    // Hide the controls in picture-in-picture mode.
    ...
} else {
    // Restore the playback UI based on the playback status.
    ...
}
}

关于您的 addflags 调用,请尝试以下代码:

@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode);

if (!isInPictureInPictureMode) {
  getApplication().startActivity(new Intent(this, getClass())
    .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
}
}
于 2020-03-24T08:05:12.063 回答