当用户在观看视频时决定按“主页”键,我希望能够保持后台播放。
我按照这里的指南: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))
并不能得到想要的结果。这让我很困惑。
- 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.
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!"); }
曾经有一段时间,我确信背景播放的结果是来自于其他操作,但当我注释掉之前提到的代码片段时,我没有得到想要的结果(如果不注释掉,我会得到想要的结果)。
if (! requestVisibleBehind(true))
及其相应的闭合大括号注释掉,那么stopPlayback()
将被调用(停止播放)。顺便说一下。 - rogerdpack