2012-03-14 64 views
2

如何创建一个线程,每秒会检查歌曲位置并根据歌曲位置移动搜索栏。我使用的打击代码是游戏只有两个songs..then这是从我的applicatin敬而远之线程在安卓媒体播放器上使用seekbar

public class setp implements MediaPlayer.OnPreparedListener 
     { 

      public void onPrepared(MediaPlayer mps) { 
       // TODO Auto-generated method stub 
       seekbar.setMax(mp.getDuration()); 
       new Thread(new Runnable() { 

        public void run() { 
          while(mp!=null && mp.getCurrentPosition()<mp.getDuration()) 
          { 
           seekbar.setProgress(mp.getCurrentPosition()); 
           Message msg=new Message(); 
           int millis = mp.getCurrentPosition(); 

           msg.obj=millis/1000; 

           try { 
            Thread.sleep(100); 
           } 
           catch (InterruptedException e) { 
            e.printStackTrace(); 
           } 
          } 
        } 
      }).start(); 
      } 
+0

我有第二首歌playing..in这之后的厚望while循环条件while(mp!= null && mp.getCurrentPosition() Palaniraja 2012-03-14 13:09:26

+0

您应该与我们分享您的堆栈跟踪。我们并不擅长猜测...... – WarrenFaith 2012-03-14 13:15:53

回答

2

下面的编码工作正常

public class setp implements MediaPlayer.OnPreparedListener { 

    public void onPrepared(MediaPlayer mps) { 
     // TODO Auto-generated method stub 
     seekbar.setMax(mp.getDuration()); 
     System.out.println("curpos" + mp.getCurrentPosition()); 
     new Thread(new Runnable() { 
      public void run() { 
       try { 
        while (mp != null && mp.getCurrentPosition() < mp.getDuration()) { 
         seekbar.setProgress(mp.getCurrentPosition()); 
         Message msg = new Message(); 
         int millis = mp.getCurrentPosition(); 

         msg.obj = millis/1000; 

         try { 
          Thread.sleep(100); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
          System.out.println("interrupt exeption" + e); 
         } 
        } // end while 
       } catch (Exception e) { 
        e.printStackTrace(); 
        System.out.println("my Exception" + e); 
       } 
      } 
     }).start(); 
    } 
} 
+0

你是否在服务中使用过它? – 2014-08-01 05:46:19

+0

yes.i在我的应用程序中使用了此代码 – Palaniraja 2014-08-01 05:55:48