Play and Stop in One button

Posted by Ardi on Stack Overflow See other posts from Stack Overflow or by Ardi
Published on 2012-10-05T09:33:32Z Indexed on 2012/10/05 9:37 UTC
Read the original article Hit count: 414

Filed under:

i'm newbie, i'm tried to make play audio play and stop for 1 button only, but i'm in trouble now.

if i touch a button when audio is playing, it doesn't stop, even playing audio again and make a double sound.

here's my code

public class ProjectisengActivity extends Activity{
    ImageButton mainkan;
    MediaPlayer mp;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test2);

        mainkan=(ImageButton)findViewById(R.id.imageButton1);
        mainkan.setOnClickListener(new OnClickListener(){
             @Override
            public void onClick(View v){

                go();
             }

            });

         public void go(){
    mp=MediaPlayer.create(ProjectisengActivity.this, R.raw.test);
    if(mp.isPlaying()){
        mp.stop();
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mp.seekTo(0);
}
    else {
    mp.start();

    }

i'm create for android 3.0 (HoneyComb) thanks for help

© Stack Overflow or respective owner

Related posts about android