2012-07-09 29 views
1

我正在開發一個應用程序,該應用程序應該可以捕獲音頻(FM/Music)的開始和結束時間。我衝浪了很多,並知道這可以通過audioFocusListener來實現。我已經實現了監聽器,但我沒有正確地獲取值。即當音頻獲得焦點並失去焦點時,這並沒有給予適當的價值。 OnAudioFocusChangeListener is not executed when the audio is played and paused?
有沒有其他的方法來實現相同的?如何在android中查找音樂應用程序的開始和結束時間?

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

你可以得到可以通過getduration()方法來實現音頻的結束時間。 – AkashG 2012-07-09 11:00:11

+0

@Aashash:謝謝,但事情是我需要調用mediaPlayer.getDuration()方法在一定的時間間隔。但是我無法獲得音樂停止播放的時間? – pushpa 2012-07-09 11:07:06

+0

嗨,你可以發佈你的OnAudioFocusChangeListener代碼來捕獲音頻的開始和結束時間。 – 2012-07-09 11:10:36

回答

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); 
     } 
    }; 
+0

@ Hasmukh:感謝您的回覆,您展示的方法是我目前的方法,但我想避免線程持續運行。所以尋找替代方法。 – pushpa 2012-07-09 11:33:48

相關問題