android 开发者后台,实现后台播放  |  Android 开发者  |  Android Developers

在 TV 设备上观看内容的用户可能会随时决定切换到 TV 启动器。如果用户在使用 TV 播放应用时切换到启动器,默认情况下,此应用会暂停。由于用户没有明确要求暂停播放,因此这种默认行为可能会显得非常突然而且出乎意料。本课程介绍如何在您的应用中启用后台播放功能,以提供更好的用户体验。

请求后台播放

通常,在用户点击主屏幕以显示 TV 启动器后,Activity 便会暂停。但是,您的应用可以请求后台播放,此时 Activity 会在 TV 启动器后面继续播放。

如需请求后台播放,请调用 false 表示请求失败,或者系统调用

Kotlin

override fun onPause() {

super.onPause()

if (videoView?.isPlaying == true) {

// 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 fun onVisibleBehindCanceled() {

// App-specific method to stop playback and release resources

stopPlayback()

super.onVisibleBehindCanceled()

}Java

@Override

public void onPause() {

super.onPause();

if (videoView.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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值