ユーザーはテレビデバイスでコンテンツを視聴しているとき、途中でテレビ ランチャーに切り替えることがあります。 テレビ再生アプリの使用中にランチャーに切り替えると、アプリはデフォルトでは一時停止します。 ユーザーは再生の一時停止を明示的にリクエストしたわけではないので、デフォルトの動作は突然の予期しない動作のように感じられるかもしれません。このレッスンでは、アプリでバックグラウンド再生を有効にして、ユーザー エクスペリエンスを向上させる方法について説明します。
requestVisibleBehind()
メソッドは API レベル 26 で非推奨になりました。
今後のリリースで削除される予定です。このページで説明している機能は、Android バージョン 8.0 以降ではサポートされていません。
バックグラウンド再生をリクエストする
通常、ユーザーが [ホーム] をクリックしてテレビ ランチャーを表示すると、アクティビティは一時停止します。ただし、アプリからバックグラウンド再生をリクエストして、再生アクティビティがテレビ ランチャーの背後で継続するようにすることができます。
バックグラウンド再生をリクエストするには、requestVisibleBehind()
を呼び出します。
アクティビティが表示されなくなった場合は、メディア リソースをクリーンアップしてください。たとえば、requestVisibleBehind()
がリクエストの失敗を示す false
を返した場合や、システムが onVisibleBehindCanceled()
のオーバーライドを呼び出した場合は、メディア リソースを解放する必要があります。
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(); }