在暂停并恢复我的静态媒体播放器类后,我无法获得audiofocus。应用获得关注并立即失去关注。我认为这是因为我的AUDIOFOCUS_LOSS外壳不好,但我不知道如何修复。我在应用中定位了API 20。
编辑:当应用程序丢失并从其他应用程序获得焦点并恢复音乐时,一切正常工作。
public class AudioManagerHandler {
private Context context;
private static final String TAG = "AudioManagerHandler";
public AudioManagerHandler(Context context){
this.context = context;
}
public boolean AudioListener(){
final AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
final AudioManager.OnAudioFocusChangeListener afChangeListener = new AudioManager.OnAudioFocusChangeListener(){
@Override
public void onAudioFocusChange(int i) {
switch(i){
case AudioManager.AUDIOFOCUS_GAIN:
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
MediaPlayerHandler.pause();
break;
case AudioManager.AUDIOFOCUS_LOSS:
MediaPlayerHandler.pause();
audioManager.abandonAudioFocus(this);
break;
}
}
};
int focusRequest = audioManager.requestAudioFocus(afChangeListener,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
if(focusRequest==AudioManager.AUDIOFOCUS_REQUEST_GRANTED){
return true;
}else{
return false;
}
}
我的日志:
07-05 20:26:01.610 4183-4183/com.example.michael.myapplication D/AudioManager: AudioManager dispatching onAudioFocusChange(-1) for android.media.AudioManager$16eb3b8com.example.michael.myapplication.AudioManagerHandler$1$f34c591
如果有任何建议,我将不胜感激。