问题
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