2013-07-12 109 views
0

我有一個應用程序,使用android媒體播放器MIDI文件播放。我製作了一個textview作爲計時器,它將根據播放進度動態改變。然而,這是非常錯誤,因爲它成爲停留在2秒大關Android的Buggy媒體播放定時器

class progressTrack extends AsyncTask<String,Void,String>{ 

      @Override 
      protected String doInBackground(String... arg0) { 
       ToggleButton mStartStop; 
       int currentPosition=0; 
       int total=mediaPlayer.getDuration(); 
       TextView timer=(TextView) findViewById(R.id.timer); 
       while (mediaPlayer != null && currentPosition < total) { 
        try { 
         Thread.sleep(1000); 
         currentPosition = mediaPlayer 
           .getCurrentPosition(); 
         double seconds=currentPosition/1000; 
         int time= (int) Math.round(seconds); 
         String timeS=Integer.toString(time); 
         Log.d("Time",timeS); 
         timer.setText(timeS+"s"); 
         fSlider.setProgress(currentPosition); 
        } catch (InterruptedException e) { 

        } catch (Exception e) { 

        } 
        OnSeekBarChangeListener listener = new OnSeekBarChangeListener() { 
         public void onStopTrackingTouch(SeekBar seekBar) { } 
         public void onStartTrackingTouch(SeekBar seekBar) { } 
         public void onProgressChanged(SeekBar seekBar, 
           int progress, 
           boolean fromUser) { 
          if(fromUser) 
           mediaPlayer.seekTo(progress); 
         } 
        }; 
        fSlider.setOnSeekBarChangeListener(listener); 
       } 

的時間確實在logcat中正確顯示,但爲什麼不會改變它在TextView的?

解決:

protected void onPostExecute(String result) { 

       ScheduledExecutorService myScheduledExecutorService = Executors.newScheduledThreadPool(1); 
       myScheduledExecutorService.scheduleWithFixedDelay(
         new Runnable(){ 
          @Override 
          public void run() { 
           monitorHandler.sendMessage(monitorHandler.obtainMessage()); 
          }}, 
            200, //initialDelay 
            200, 
            TimeUnit.MILLISECONDS//delay         TimeUnit.MILLISECONDS 
         );                       
      }   
} 

Handler monitorHandler = new Handler(){ 

     @Override 
     public void handleMessage(Message msg) { 
     startUI(); 
     } 

}; 

void startUI(){ 
     Log.d("UI", "Started player UI"); 
     ToggleButton mStartStop; 
     int currentPosition=0; 
     int total=mediaPlayer.getDuration(); 
     TextView timer=(TextView) findViewById(R.id.timer); 
     if(mediaPlayer.isPlaying()) 
      try { 
       currentPosition = mediaPlayer 
         .getCurrentPosition(); 
       double seconds=currentPosition/1000; 
       int time= (int) Math.round(seconds); 
       String timeS=Integer.toString(time); 
       timer.setText(timeS+"s"); 
       Log.d("position",timeS); 
       fSlider.setProgress(currentPosition); 
      } catch (Exception e) { 

      } 
      OnSeekBarChangeListener listener = new OnSeekBarChangeListener() { 
       public void onStopTrackingTouch(SeekBar seekBar) { } 
       public void onStartTrackingTouch(SeekBar seekBar) { } 
       public void onProgressChanged(SeekBar seekBar, 
         int progress, 
         boolean fromUser) { 
        if(fromUser) 
         mediaPlayer.seekTo(progress); 
       } 
      }; 
      fSlider.setOnSeekBarChangeListener(listener); 

     mStartStop= (ToggleButton) findViewById(R.id.toggleButton1); 
     mStartStop.setOnCheckedChangeListener(pauseListener); 

} 

回答

0

你需要做的這一個單獨的線程,並通過runOnUiThread回發到GUI線程與runnable設置你的計時器的文字。正如你大概設置一個GUI組件的文本無一例外,我認爲你現在沒有這樣做。

請發表完整的方法。用完整的方法,我們可以幫助追蹤爲什麼它停在2秒的標記。

+0

我剛貼完全班 – StephenChen