当电视正在播放频道并且我的应用程序启动并在进入后台时失去焦点时,我需要我的应用程序来获得音频焦点。我试过了,但不能在谷歌电视上工作。我试过 mAudioManager.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN); 它在 device.ie 中工作的音乐在应用程序启动时停止。但不适用于带有任何参数的谷歌电视。音频焦点的步骤是:
1.在主应用程序中
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
requestFocus();
在 onresume()
protected void onResume() { super.onResume(); requestFocus(); }
3.在 onStop()
protected void onStop(){
abandonFocus();
super.onStop();
Log.e("APPLICATION", "APPLICATION onStop");
}
4.在 onDestroy()
public void onDestroy(){
super.onDestroy();
//Log.e("APPLICATION", "APPLICATION onDestroy");
abandonFocus();
}
5.AudioFocus的方法。
/** Requests audio focus. Returns whether request was successful or not. */
public boolean requestFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_DTMF, AudioManager.AUDIOFOCUS_GAIN);
}
/** Abandons audio focus. Returns whether request was successful or not. */
public boolean abandonFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.abandonAudioFocus(this);
}
/**
* Called by AudioManager on audio focus changes. We implement this by calling our
* MusicFocusable appropriately to relay the message.
*/
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
Log.e("AUDIOFOCUS","AUDIOFOCUS_GAIN");
break;
case AudioManager.AUDIOFOCUS_LOSS:Log.e("AUDIOFOCUS","AUDIOFOCUS_LOSS");
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:Log.e("AUDIOFOCUS","AUDIOFOCUS_LOSS_TRANSIENT");
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:Log.e("AUDIOFOCUS","AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
break;
default:
}
}
任何人都可以在这方面帮助我。谁在谷歌电视中做了音频焦点。
谢谢,巴拉特