user2407147
user2407147

Reputation: 1550

ProgressBar does not reset after audio is finished done

I have asked this question 2 times now still haven't got it to work. Any help would be awesome

My ProgressBar does not reset after audio is done, the bar just stays to the max blue line. I ask a question before on this and got it working but now just stopped working and not sure why it doesn't. Any help would be awesome.

All I want is it to chose a audio at random then play one and when finished you can press play again to listen to the same audio it chose at random.

Heres code:

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

Upvotes: 0

Views: 912

Answers (1)

free3dom
free3dom

Reputation: 18978

I did not check all the code thoroughly, but at a quick glance I would guess that your thread (which updates the progress bar) is stopping at completion and you never start it again (ie. when the user clicks play again). Just try restarting the thread in your pauseicon.setOnClickListener (when playback is complete). Example:

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

EDIT using a static variable to store thread so that it can be restarted from the view's onClick method:

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

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

If this does not work (I can't test it out right now), you can simply keep the thread running, for example:

// 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; 

Upvotes: 1

Related Questions