完成音频后,ProgressBar不会重置

时间:2013-08-31 14:24:33

标签: android audio android-mediaplayer android-progressbar

我已经问过这个问题2次现在仍然没有得到它的工作。任何帮助都很棒

完成音频后,我的ProgressBar不会重置,栏会保持最大蓝线。我之前问了一个问题,并让它工作但现在只是停止工作,不知道为什么它没有。任何帮助都会很棒。

我想要的是随机选择一个音频然后播放一个音频,完成后你可以再次按下播放来听取它随机选择的同一个音频。

Heres代码:

 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 个答案:

答案 0 :(得分:1)

我没有彻底检查所有代码,但是我很快就会猜到你的线程(更新进度条)在完成时停止,你再也不会启动它(即用户再次点击播放时) 。只需尝试重新启动pauseicon.setOnClickListener中的线程(播放完成时)。例如:

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();
      }
   }
});

编辑使用静态变量来存储线程,以便可以从视图的onClick方法重新启动它:

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

// add this to BOTH onCreate and onClick
progressThread.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;