在Android TV上启用后台播放

7

当用户在观看视频时决定按“主页”键,我希望能够保持后台播放。

我按照这里的指南:https://developer.android.com/training/tv/playback/options.html执行,并编写了以下代码(已经生效):

@Override
public void onPause() {
    super.onPause();
    if (mVideoView.isPlaying())
    {
        // Argument equals true to notify the system that the activity
        // wishes to be visible behind other translucent activities
        if (! requestVisibleBehind(true)) {
            // App-specific method to stop playback and release resources
            // because call to requestVisibleBehind(true) failed
            stopPlayback();
        }
    } else {
        // Argument equals false because the activity is not playing
        requestVisibleBehind(false);
    }
}

@Override
public void onVisibleBehindCanceled() {
  // App-specific method to stop playback and release resources
  stopPlayback();
  super.onVisibleBehindCanceled();
}

我有几个问题。 我发现注释行 if (! requestVisibleBehind(true)) 并不能得到想要的结果。这让我很困惑。

  1. Shouldn't that line return some boolean value? How can it enable background playback? With the debugger, mentioned "if" is successful and it stops the player. Not really sure what's happening and how, so maybe someone can explain me.
  2. requestVisibleBehind() is @deprecated, so is onVisibleBehindCanceled(). Is there any alternatives? When I tried to look into what these functions hold, I also surprised myself when I found (same goes for requestVisibleBehind().

    public void onVisibleBehindCanceled() 
    {
        throw new RuntimeException("Stub!");
    }
    

曾经有一段时间,我确信背景播放的结果是来自于其他操作,但当我注释掉之前提到的代码片段时,我没有得到想要的结果(如果不注释掉,我会得到想要的结果)。


{btsdaf} - chandrakant sharma
1
如果你将 if (! requestVisibleBehind(true)) 及其相应的闭合大括号注释掉,那么 stopPlayback() 将被调用(停止播放)。顺便说一下。 - rogerdpack
这个链接可能对您有所帮助:http://corochann.com/android-tv-application-hands-on-tutorial-9-195.html - Rakesh Gupta
1个回答

3
我不确定我是否正确理解了你的问题。根据谷歌,requestVisibleBehind(true) 请求安卓操作系统,在进入后台时该活动应继续播放,它返回的布尔值是你是否可以指望它继续播放(由操作系统决定)。 关于onVisibleBehindCanceled(),当应用程序由用户或操作系统完成或者requestVisibleBehind返回false时调用,它基本上会在500毫秒内为您清除应用程序使用的所有资源,之后操作系统将销毁您的活动以释放这些资源。 因此,总结起来,这些确实是使您的应用程序继续在后台播放的原因。 关于弃用,请记住,安卓O版提供了使用PIP的功能,可能这就是更改的原因。
希望对你有所帮助。

谢谢,对于Android TV,您可能不想使用PIP,但仍希望在后台显示完整的视频。 - Hrk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接