我正在开发一个应用程序,它应该捕获音频(FM /音乐)的开始和结束时间。我上网了很多,知道这可以通过 audioFocusListener 来实现。我已经实现了监听器,但我没有正确获取值。即当音频获得焦点并失去焦点时,这没有给出适当的值。
OnAudioFocusChangeListener 在播放和暂停音频时没有执行?
有没有其他方法可以实现相同的?
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);
am.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN)
}
@Override
public void onAudioFocusChange(int focusChange) {
Log.d("AudioManager", "Inside on audio focus change");
if(focusChange == AudioManager.AUDIOFOCUS_GAIN)
Log.d("AudioManager", "audio focus gained");
if(focusChange == AudioManager.AUDIOFOCUS_LOSS)
Log.d("AudioManager", "audio focus lossed");
/* Here i am always getting value -1 */
}
}
提前致谢