2013-08-31 30 views
0

我已经问了2次这个问题,现在还没有完成它的工作。任何帮助将是真棒音频完成后,ProgressBar不会重置

音频完成后,我的ProgressBar不会重置,酒吧只停留在最大蓝线。我在此之前提出一个问题,并让它工作,但现在只是停止工作,不知道为什么没有。任何帮助都是极好的。

我只需要随机选择一个音频,然后播放一个,当完成后,您可以再按一次播放来听它随机选择的相同音频。

继承人代码:

public class player2 extends Activity implements Runnable { 


private MediaPlayer mp; 
private ProgressBar progressBar; 
private ImageButton pauseicon; 
private final int NUM_SOUND_FILES = 3; //*****REPLACE THIS WITH THE ACTUAL NUMBER OF SOUND FILES YOU HAVE***** 
private int mfile[] = new int[NUM_SOUND_FILES]; 
private Random rnd = new Random(); 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.player_2); 
     pauseicon = (ImageButton) findViewById(R.id.pauseicon); 
     progressBar = (ProgressBar) findViewById(R.id.progressBar); 
     getActionBar().setDisplayHomeAsUpEnabled(true); 



     mfile[0] = R.raw.sound04; //****REPLACE THESE WITH THE PROPER NAMES OF YOUR SOUND FILES 
     mfile[1] = R.raw.sound05; //PLACE THE SOUND FILES IN THE /res/raw/ FOLDER IN YOUR PROJECT***** 
     mfile[2] = R.raw.sound06; 
     // Listeners 
     /** 
     * Play button click event 
     * plays a song and changes button to pause image 
     * pauses a song and changes button to play image 
     * */ 


     try{ 
      mp = MediaPlayer.create(player2.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]); 
      mp.seekTo(0); 
      mp.start(); ;   
      progressBar.setVisibility(ProgressBar.VISIBLE); 
      progressBar.setProgress(0); 
      progressBar.setMax(100); 

      new Thread(this).start(); 

     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } 

     mp.setOnCompletionListener(new OnCompletionListener() { 

      public void onCompletion(MediaPlayer mp) { 
       pauseicon.setImageResource(R.drawable.playicon); 
      } 
     }); 

     pauseicon.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       // No need to check if it is pauseicon 

     if(mp.isPlaying()){ 
      mp.pause(); 
     ((ImageButton) v).setImageResource(R.drawable.playicon); 

     } else { 
      mp.start(); 
      ((ImageButton) v).setImageResource(R.drawable.pauseicon); 
    }}}); 

    } 

    public void run() { 
     int currentPosition= 0; 
     int total = mp.getDuration(); 
     while (mp!=null && currentPosition<=total) { 
      try { 
       Thread.sleep(1000); 
       currentPosition= mp.getCurrentPosition(); 
      } catch (InterruptedException e) { 
       return; 
      } catch (Exception e) { 
       return; 
      }    
      progressBar.setProgress(currentPosition); 
     } 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      NavUtils.navigateUpFromSameTask(this); 


     if (mp != null) 
     if(mp.isPlaying()) 
       mp.stop(); 

      mp.release(); 

      return true; 
     default: 
      return super.onOptionsItemSelected(item); 

    } 

     } 
    @Override 
    public void onBackPressed(){ 
     if (mp != null){ 
      if(mp.isPlaying()) 
       mp.stop(); 

      mp.release(); 
     } 

     //there is no reason to call super.finish(); here 
     //call super.onBackPressed(); and it will finish that activity for you 
     super.onBackPressed(); 
    } 
    } 

回答

1

我没有彻底检查所有的代码,但看一眼我猜想,你的线程(更新进度条)在完成停止和你永远不启动再次(即,当用户再次点击游戏时)。只需尝试重新启动pauseicon.setOnClickListener中的线程(播放完成时)。例如:

// add this to your class as a member 
static Thread progressThread = new Thread(this); 

// add this to BOTH onCreate and onClick 
progressThread.start(); 

如果这不起作用(我可以”:使用静态变量来存储线程,以便它可以从视图的onClick方法重新启动

pauseicon.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     if(mp.isPlaying()) { 
     mp.pause(); 
     ((ImageButton) v).setImageResource(R.drawable.playicon); 
     } else { 
     mp.start(); 
     ((ImageButton) v).setImageResource(R.drawable.pauseicon); 
     // RESTART THE UPDATE THREAD // 
     new Thread(this).start(); 
     } 
    } 
}); 

编辑现在就测试一下),你可以简单地保持线程运行,例如:

// flag to set when thread should be actively running 
static boolean runThread = true; 

// change your run method to something as follows 
public void run() { 
    while (runThread) { 
     if (mp != null && currentPosition <= total) { 
      int currentPosition= 0; 
      int total = mp.getDuration(); 
      try { 
      Thread.sleep(1000); 
      currentPosition= mp.getCurrentPosition(); 
      } catch (InterruptedException e) { 
      return; 
      } catch (Exception e) { 
      return; 
      }    
      progressBar.setProgress(currentPosition); 
     } 
     else 
      Thread.sleep(1000); 
    } 
} 

// then when you no longer need to update the progress bar set the flag to false, 
// which will cause your thread to finish. this can go anywhere, depending on 
// your needs 
runThread = false; 
+0

我得到这个错误构造函数Thread(new View.OnClickListener(){})未定义。但谢谢你的尝试,任何建议:) – user2407147

+0

当然,这不会工作...我的坏;)'onClick'实际上是在'View'类,而不是你的活动。如果你使用'new Thread(player2).start();',它应该可以工作。对困惑感到抱歉。 – free3dom

+0

现在得到这个错误,player2无法解析为变量。 :S – user2407147

相关问题