2015-04-26 91 views
1

使用媒體播放器,我在做什麼:我從一個urlandroid系統

這是怎麼回事播放的媒體播放器:一開始,畫面就像是反應遲鈍,並掛起,因爲這首歌的緩衝是發生。

問:

  1. 如何克服呢?
  2. 我是否需要在這裏使用Async任務而不是處理程序? ...如果 那麼如何修改我已發佈的代碼
  3. 或者我應該只需要在此處顯示進度對話框。在處理程序? ...有可能嗎?如果是這樣怎麼樣?

代碼:

protected void newValue(TextView txtSrcId, TextView txtDstId) { 
      // TODO Auto-generated method stub 


      do { 
       current = mediaPlayer.getCurrentPosition(); 
       System.out.println("duration - " + duration + " current- " 
         + current); 
       int dSeconds = (int) (duration/1000) % 60 ; 
       int dMinutes = (int) ((duration/(1000*60)) % 60); 
       int dHours = (int) ((duration/(1000*60*60)) % 24); 

       int cSeconds = (int) (current/1000) % 60 ; 
       int cMinutes = (int) ((current/(1000*60)) % 60); 
       int cHours = (int) ((current/(1000*60*60)) % 24); 


       if(dHours == 0){ 
        txtSrcId.setText(String.format("%02d:%02d", cMinutes, cSeconds)); 
        txtDstId.setText(String.format("%02d:%02d", dMinutes, dSeconds)); 
        //txtSrcId.setText("jodwjdwudwudguwdgwugduwgduwgduwgd"); 
       }else{ 
        txtSrcId.setText(String.format("%02d:%02d:%02d",cHours, cMinutes, cSeconds)); 
        txtDstId.setText(String.format("%02d:%02d:%02d",dHours, dMinutes, dSeconds)); 
        //txtSrcId.setText("jodwjdwudwudguwdgwugduwgduwgduwgd"); 
       } 

       try{ 
        Log.d("Value: ", String.valueOf((int) (current * 100/duration))); 
        if(seekBarProgress.getProgress() >= 100){ 
         break; 
        } 
       }catch (Exception e) {} 
      }while (seekBarProgress.getProgress() <= 100); 
     } 


     private Runnable onEverySecond = new Runnable() { 
      @Override 
      public void run(){ 
       if(true == running){ 
        if(seekBarProgress != null) { 
         seekBarProgress.setProgress(mediaPlayer.getCurrentPosition()); 
        } 

        if(mediaPlayer.isPlaying()) { 
         seekBarProgress.postDelayed(onEverySecond, 1000); 
         //updateTime(); 
         newValue(txtSrcId,txtDstId); 
        } 
       } 
      } 
     }; 

     public void play() { 
      try { 
       mediaPlayer.setDataSource(url); 
       mediaPlayer.prepare(); 
      } catch (IllegalStateException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      mediaPlayer.start(); 
      seekBarProgress.postDelayed(onEverySecond, 1000); 

     } 


     @Override 
     public void onPrepared(MediaPlayer arg0) { 
      // TODO Auto-generated method stub 
      duration = mediaPlayer.getDuration(); 
      seekBarProgress.setMax(duration); 
      seekBarProgress.postDelayed(onEverySecond, 1000); 
     } 


     @Override 
     public void onBufferingUpdate(MediaPlayer mp, int percent) { 
      /** Method which updates the SeekBar secondary progress by current song loading from URL position*/ 
      seekBarProgress.setSecondaryProgress(percent); 
     } 

注:如果需要更多的代碼,讓我知道我將編輯您正在尋找MediaPlayer#prepareAsync問題

回答

0

準備播放器進行異步播放。設置數據源和顯示界面後,您需要調用prepare()或prepareAsync()。對於流,你應該調用prepareAsync(),它會立即返回,而不是阻塞,直到有足夠的數據被緩衝。

使用此代碼而不是prepare。當媒體源已準備好通過MediaPlayer.OnPreparedListener.html播放時,您會收到通知。