0

My ProgressBar does not reset after audio is done. It reset after some audio's but not others not sure why. Also would like to make pause icon change to play when audio is done if anyone can help out with that to, that will be great!

Heres code:

 public class player1 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_1);
        pauseicon = (ImageButton) findViewById(R.id.pauseicon);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        getActionBar().setDisplayHomeAsUpEnabled(true);



        mfile[0] = R.raw.sound01;  //****REPLACE THESE WITH THE PROPER NAMES OF YOUR SOUND FILES
        mfile[1] = R.raw.sound02;  //PLACE THE SOUND FILES IN THE /res/raw/ FOLDER IN YOUR PROJECT*****
        mfile[2] = R.raw.sound03;
        // 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(player1.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]);
             mp.seekTo(0);
             mp.start(); ;         
             progressBar.setVisibility(ProgressBar.VISIBLE);
             progressBar.setProgress(0);
             progressBar.setMax(mp.getDuration());
             new Thread(this).start();

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



        pauseicon.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                 if (v.getId() == R.id.pauseicon)
        if(mp.isPlaying()){
            mp.pause();
          ImageButton pauseicon =(ImageButton) findViewById(R.id.pauseicon);

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

                 ImageButton pauseicon =(ImageButton) findViewById(R.id.pauseicon);

                 pauseicon.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);
        }


    }

    public boolean onOptionsItemSelected(MenuItem item){
        Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
        startActivityForResult(myIntent, 0);
        return true;

        }

    }
user2407147
  • 1,508
  • 2
  • 22
  • 40

1 Answers1

1

First issue:

In the thread "run" method, in the while condition you are saying:

(currentPosition<total)

this way "currentPosition" value will never reach "total" value and your progressbar won't go, you should use "less than or equal":

(currentPosition<=total)

so code will be:

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

Second issue:

You can change the ImageButton using "mp.setOnCompletionListener(this)" like this:

mp.setOnCompletionListener(new OnCompletionListener() {

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

Set it while initializing the mediaplayer, before mp.start()

Abuzaid
  • 853
  • 8
  • 28
  • Thank you so much! You have fix my first problem which was the main one so thank you so much again! But second one still occurs as a problem. At the end of audio still stays as a pause icon. Any help? Thanks for now though :D – user2407147 May 28 '13 at 18:36
  • Hey, I tried that still does not work? Any other suggestions, but that's for trying :) – user2407147 May 28 '13 at 19:07
  • Thank you so much the OnCompletionListener work! Again Thanks :D:D:D – user2407147 May 28 '13 at 19:13
  • Also you need to add a condition to exit the while loop, this way it may loop forever. Something like: while (mp!=null && mp.isPlaying() && currentPosition<=total) {...} So the loop will exit when the player stops. – Abuzaid May 28 '13 at 19:31
  • Abuzaid I need you help again with progressBar if thats ok with you? – user2407147 Aug 30 '13 at 15:32