1

我正在开发一个应用程序,它应该捕获音频(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 */

 }
}

提前致谢

4

1 回答 1

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);
       }
    };
于 2012-07-09T11:16:14.030 回答