OnAudioFocusChangeListener is not executed when the audio is played and paused?

爱⌒轻易说出口 提交于 2020-01-05 04:36:07

问题


I am developing an application, in which i need to capture the audio being played. So i implemented OnAudioFocusChangeListener in my activity to listen for any audio changes. But the listener is not getting called when music is played or paused. Below is my code

public class AudioManagerExample extends Activity implements OnAudioFocusChangeListener{
  AudioReceiver adreceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    AudioManager am =   (AudioManager) getSystemService(AUDIO_SERVICE);

}

@Override
public void onAudioFocusChange(int focusChange) {
    Log.d("AudioManager", "Inside on audio focus change");
}

}

Where am i wrong? Is there any alternative way to do this?

Thanks

Pushpa


回答1:


You forgot to include

 am.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);

Change your code to

AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
am.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);


来源:https://stackoverflow.com/questions/11081465/onaudiofocuschangelistener-is-not-executed-when-the-audio-is-played-and-paused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!