我的手机上有一个应用程序可以持续关注音频,即使没有声音播放也是如此。作为应用程序开发人员,我想知道我是否能够告知用户它是哪个应用程序,或者我是否可以判断我的应用程序是否具有音频焦点?
问问题
2980 次
2 回答
7
我强烈怀疑是否有任何公共 API 可以告诉您目前哪个应用程序具有焦点。
您可以通过请求来跟踪您的应用是否具有音频焦点,例如:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
boolean requestGranted = AudioManager.AUDIOFOCUS_REQUEST_GRANTED == audioManager.requestAudioFocus(listener, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if(requestGranted){
// you now has the audio focus
}
当您请求和放弃焦点时,您应该确保维护您的侦听器的单个实例,请在此处查看我的答案以解决音频焦点的常见问题
这是一个例子onAudioFocusChange()
:
@Override
public void onAudioFocusChange(int focus) {
switch (focus) {
case AudioManager.AUDIOFOCUS_LOSS:
Log.d(TAG,"AUDIOFOCUS_LOSS");
// stop and release your player
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.d(TAG,"AUDIOFOCUS_LOSS_TRANSIENT");
// pause your player
break;
case AudioManager.AUDIOFOCUS_GAIN:
Log.d(TAG,"AUDIOFOCUS_GAIN");
// restore volume and resume player if needed
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.d(TAG,"AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
// Lower volume
break;
}
}
于 2013-01-02T21:09:13.027 回答
0
Maybe this method does what you want: AudioManager.isMusicActive() http://developer.android.com/reference/android/media/AudioManager.html#isMusicActive%28%29
于 2021-01-25T00:45:08.390 回答