2012-03-13 77 views
0

如何在android mediplayer中使用seekbar?我使用了下面的代碼。 seekbar在firstsong工作,然後離開我的應用程序。安卓mediabar與seekbar

mp = MediaPlayer.create(player.this, (Integer) plsong.get(i)); 
         mp.start(); 

      mp.setOnCompletionListener(new OnCompletionListener() { 

      public void onCompletion(MediaPlayer player) { 
       mp.release(); 
        sname.setText(" "); 
       if (j < (plsong.size() - 1)) { 
         j++; 
         mp = MediaPlayer.create(player.this,(Integer) plsong.get(j)); 
         sname.setText((CharSequence) soname.get(j)); 
         mp.start(); 
         mp.setOnCompletionListener(this); 


        }   

      } 
     }); 
     mp.setOnPreparedListener(new OnPreparedListener() { 

      public void onPrepared(MediaPlayer mpss) { 
       // TODO Auto-generated method stub 
       total = mp.getDuration(); 
       System.out.println("total" + total); 
      } 
     }); 
      seekbar.setProgress(0); 
      seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 

       public void onStopTrackingTouch(SeekBar arg0) { 
        // TODO Auto-generated method stub 

       } 

       public void onStartTrackingTouch(SeekBar arg0) { 
        // TODO Auto-generated method stub 

       } 

       public void onProgressChanged(SeekBar seekBar, int progress, 
         boolean fromUser) { 
        // TODO Auto-generated method stub 

        if (fromUser) { 
         mp.seekTo(progress); 
         seekBar.setProgress(progress); 
        } 
       } 
      }); 
      Thread currentThread = new Thread(MyCounter.this); 
      currentThread.start(); 
      mplay = (Button) findViewById(R.id.mplay); 
      if(!mp.isPlaying()) 
       { 

      mplay.setText("Pause"); 
      mplay.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        mp.start(); 
        seekbar.setMax(total); 
       } 
      }); 
       } 
      else 
      { 
       mplay.setText("play"); 
       mplay.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v) { 
         // TODO Auto-generated method stub 

         mp.pause(); 

        } 
       }); 
      } 


     } 
     public void run() { 
       // TODO Auto-generated method stub 

       try { 
        while (mp != null) { 
         int currentPosition = mp.getCurrentPosition(); 
         Message msg = new Message(); 
         msg.what = currentPosition; 
         threadHandler.sendMessage(msg); 
        } 
        Thread.sleep(100); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 

      private Handler threadHandler = new Handler() { 
       public void handleMessage(Message msg) { 
        seekbar.setProgress(msg.what); 
       } 
      }; 
+0

Hi..give我一個解決方案..... – Palaniraja 2012-03-14 05:14:29

回答

3

刪除您創建移動搜索條中的所有線程和添加這些:

mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener(){ 
public void onBufferingUpdate(MediaPlayer mp, int percent) { 
SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar); 
if (mp.isPlaying() && seekBar!=null) { 
seekBar.setProgress(mp.getCurrentPosition()); 
} 

} 
});