我正在开发一个应该捕获音频(FM /音乐)的开始和结束时间的应用程序。我经常浏览并知道这可以通过audioFocusListener来实现。我已经实现了监听器,但我没有正确获取值。即当音频获得焦点并失去焦点时,这没有给出适当的值。
OnAudioFocusChangeListener is not executed when the audio is played and paused?
有没有其他方法可以实现相同的目标?
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 */
}
}
提前致谢
答案 0 :(得分:0)
我在这里粘贴了一些代码,以便您可以根据自己的要求进行操作。 希望这会对你有所帮助,
public void updateProgressBar()
{
mHandler.postDelayed(mUpdateTimeTask, 100);
}
/**
* Background Runnable thread
* */
private Runnable mUpdateTimeTask = new Runnable()
{
public void run()
{
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
// Displaying Total Duration time
songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
// Displaying time completed playing
songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));
// Updating progress bar
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
//Log.d("Progress", ""+progress);
songProgressBar.setProgress(progress);
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};