2013-04-25 21 views
1

我試圖使用媒體播放器播放.wav文件。其實我已經做了如下所示。它正確地更新了進度,但問題在於,媒體播放器並沒有完全求得。媒體播放器停止在文件的總持續時間之前發佈進度並停止更新進度。我還實現了OnComplete監聽器,但我沒有收到回調。在這裏,我的代碼可以讓我發現哪裏有缺失的地方。幫我......Android媒體播放器沒有完全求進步

public void playAudioFile() { 
    try { 

     mMediaPlayer.reset(); 
     setMediaPlayerSource(); 
     mMediaPlayer.prepare(); 
     mMediaPlayer.start(); 
     mMediaPlayer.setOnCompletionListener(new OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer arg0) { 
       Graphbar.setProgress(mMediaPlayer.getDuration()); 
      } 
     }); 
     bPlay.setBackgroundResource(R.drawable.pause_selector); 

     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       System.out.println("Playing"); 
       updateProgressBar(); 
      } 
     }).start(); 

    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
    public void updateProgressBar() { 
    Graphbar.setProgress(0); 

    Graphbar.setMax(mMediaPlayer.getDuration()); 
    mHandler.postDelayed(mUpdateTimeTask, 0); 
} 
private Runnable mUpdateTimeTask = new Runnable() { 
    public void run() { 
     if(mMediaPlayer.isPlaying()){ 
     long totalDuration = Graphbar.getMax(); 
     long currentDuration =mMediaPlayer.getCurrentPosition(); 

     String TotalDur = Utilities.milliSecondsToTimer(totalDuration); 

     tvTimeRight.setText(TotalDur); 
     tvTimeLeft.setText(""+ Utilities.milliSecondsToTimer(currentDuration)); 

     System.out.println(currentDuration+"/"+totalDuration); 
     Graphbar.setProgress((int)currentDuration); 
     mHandler.postDelayed(this, 1); 
     } 
    } 
}; 
private void setMediaPlayerSource() { 
     String audioFile = getFilename(); 
     try { 
      mMediaPlayer.setDataSource(audioFile); 
     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

回答

0

總的持續時間使用mediaplayer.getduration。 從可運行處理程序中刪除不必要的代碼,因爲它會爲您的搜索欄創建加載。因爲在每秒你正在計算的東西和更新佈局根據該計算。

我建議你使用天文臺查看android。

public void playAudioFile() { 
    try { 

     mMediaPlayer.reset(); 
     setMediaPlayerSource(); 
     mMediaPlayer.prepare(); 
     mMediaPlayer.start(); 
     mMediaPlayer.setOnCompletionListener(new OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer arg0) { 
       Graphbar.setProgress(mMediaPlayer.getDuration()); 
      } 
     }); 
     bPlay.setBackgroundResource(R.drawable.pause_selector); 
     //set this only for once , because for one song total duration is always constant. 

    updateProgressBar(); 
} 
    public void updateProgressBar() { 
    Graphbar.setProgress(0); 

    Graphbar.setMax(mMediaPlayer.getDuration()); 
    mHandler.postDelayed(mUpdateTimeTask, 0); 

    long totalDuration = mMediaPlayer.getDuration(); 
    String TotalDur = Utilities.milliSecondsToTimer(totalDuration); 

     tvTimeRight.setText(TotalDur); 
} 
private Runnable mUpdateTimeTask = new Runnable() { 
    public void run() { 

     if(mMediaPlayer.isPlaying()){ 
     //long totalDuration = mMediaPlayer.getDuration();=> no need of calculating total time and updating it to seekbar at every second 

     long currentDuration =mMediaPlayer.getCurrentPosition(); 


     tvTimeLeft.setText(""+ Utilities.milliSecondsToTimer(currentDuration)); 

     System.out.println(currentDuration+"/"+totalDuration); 
     Graphbar.setProgress((int)currentDuration); 
     mHandler.postDelayed(this, 1); 
     } 
    } 
}; 
private void setMediaPlayerSource() { 
     String audioFile = getFilename(); 
     try { 
      mMediaPlayer.setDataSource(audioFile); 
     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    }