Question

I have a mediaplayer in my activity and I have two buttons to play next and previous sounds. I want when the user tabs on next or previous button the sound stop playing and show some detail about the next or previous sound and by clicking on the play button it start to play, to do that I used reset(). but when I want to play the next sound it doesn't play.

package com.example.listeninglevel1;

public class PagesActivity extends SherlockActivity implements Runnable, OnClickListener, OnSeekBarChangeListener{
SubMenu sm;
public static int pos=1, lastPage=5;
ImageButton next, back, nextNoneSelect, backNoneSelect;
String txstory;
TextView txview;
public static RelativeLayout relSeek, relMain;
private String[] title = new String[6];
private SeekBar seekBar;
private ImageButton startMedia;
private ImageButton pauseMedia;
public static MediaPlayer mp = new MediaPlayer();

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.Theme_Sherlock_Light);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pages);

    AudioControl();   

    next = (ImageButton) findViewById(R.id.next);
    next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (pos < lastPage) {
                pos++;
            } 
            mp.reset();
            pauseMedia.setVisibility(View.INVISIBLE);
            startMedia.setVisibility(View.VISIBLE);
        }
    });

    back = (ImageButton) findViewById(R.id.back);
    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (pos > 1) {
                pos--;
            }
            mp.reset();
            pauseMedia.setVisibility(View.INVISIBLE);
            startMedia.setVisibility(View.VISIBLE);
        }
    });

}

 public void AudioControl(){
        seekBar = (SeekBar) findViewById(R.id.seekBar1);
        startMedia = (ImageButton) findViewById(R.id.play);
        pauseMedia = (ImageButton) findViewById(R.id.pause);
        seekBar.setOnSeekBarChangeListener(this);
        startMedia.setOnClickListener(this);
        pauseMedia.setOnClickListener(this); 
    }

    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;
            }            
            seekBar.setProgress(currentPosition);
        }
    }

    public void onClick(View v) {
        pauseMedia.setVisibility(View.VISIBLE);
        seekBar.setVisibility(View.VISIBLE);
        startMedia.setVisibility(View.INVISIBLE);
        if (v.equals(startMedia)) {
            if (mp != null && mp.isPlaying()) return;
            if(seekBar.getProgress() > 0) {
                mp.start();
                return;
            }
            int[] arr = {R.raw.sound1,R.raw.sound2,R.raw.sound3,R.raw.sound4,R.raw.sound5};
            int snum = pos -1;
            mp = MediaPlayer.create(PagesActivity.this, arr[snum]);
            mp.start();            

            seekBar.setProgress(0);
            seekBar.setMax(mp.getDuration());
            new Thread(this).start();
        }

        if (v.equals(pauseMedia) && mp!=null) {
            mp.pause();
            pauseMedia.setVisibility(View.INVISIBLE);
            startMedia.setVisibility(View.VISIBLE);
        }       
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mp!=null) {
            mp.pause();
        }  
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        if(fromUser) mp.seekTo(progress);

    }
}
Was it helpful?

Solution

You can try with

mp.pause();
mp.seekTo(0);

This will pause MP player and return song to start. And next time you click, it will play Media Player from beginning.

if (mp.isPlaying()){
                    mp.pause();
                    mp.seekTo(0);
                }
                else{
                    mp.start();
                }
                }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top