1

I have 2 sound clips 19 sec each on the click of button1 Soundclip1 should play and on the click of button2 the playing Soundclip1 should stop and Soundclip2 should start.. Please help me i am stuck. i research about this a lot but my problem is both of the sound playing simultaneously....

Please.... I have refered this Android Media Player Wont Play After Stop and Stop the prevoius sound before starting a new sound in my android app

Community
  • 1
  • 1
Abhi
  • 433
  • 2
  • 7
  • 17
  • ss this http://stackoverflow.com/questions/11507062/play-two-different-sound-files-from-two-different-buttons – rajshree Feb 28 '14 at 10:10
  • see this http://stackoverflow.com/questions/21555637/grid-view-and-media-player-on-click/21556442#21556442 – Sagar Shah Feb 28 '14 at 10:11
  • You can combine this http://stackoverflow.com/questions/21560606/music-play-but-not-pause-on-android/21560726#21560726 with my above link. – Sagar Shah Feb 28 '14 at 10:13
  • did you work on it,post code – appukrb Feb 28 '14 at 10:13
  • Sory Rajashree but it is giving an error on MediaPlayer.create... – Abhi Feb 28 '14 at 10:16
  • case R.id.ibPlayh: ibPlay.startAnimation(aBellPlay); if(ibPlay.isPressed()){ i++; } else if(i > 1){ mpNamyoho.reset(); } mpNamyoho.start(); budhamnamyo(mpBuddhamKsharanam); case R.id.ibPlayh2: ibPlay2.startAnimation(aBellPlay2); if(ibPlay2.isPressed()){ j++; } else if(j > 1){ mpNamyoho.reset(); } mpBuddhamKsharanam.start(); budhamnamyo(mpNamyoho); protected void budhamnamyo(MediaPlayer mp) { if(mp != null && mp.isPlaying()){ if(ibPlay.isPressed()){ mpBuddhamKsharanam.stop(); } else if(ibPlay2.isPressed()){ mpNamyoho.stop(); } } – Abhi Feb 28 '14 at 10:22
  • this code works only once if i press button another time both the sound clip are not playing – Abhi Feb 28 '14 at 10:26
  • actually sound clips are 19 sec long – Abhi Feb 28 '14 at 10:28

1 Answers1

1

Try this-

final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.one);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.two);

Button btn = (Button)this.findViewById (R.id.button1);
btn.setOnClickListener(new OnClickListener(){
    @override
    public void onClick(View v) {
        mp1.start();
        mp2.pause();
        mp2.seekTo(0);
    }
});

Button btn1 = (Button)this.findViewById (R.id.button2);
btn1.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        mp1.pause();
        mp1.seekTo(0);
        mp2.start();
    }
});
Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21
  • Ya but it is played only once not all the time – Abhi Feb 28 '14 at 10:42
  • @Abhi I have tried similar code. Its working. Once try. – Kanwaljit Singh Feb 28 '14 at 10:44
  • Ya dear but i want to start the clip from the begning.. it actually pauses Please Help me i am a newbie in android – Abhi Feb 28 '14 at 10:51
  • @Abhi Use **mp2.stop();** instead of **mp2.pause();**. Similarly for **mp1.pause();** – Kanwaljit Singh Feb 28 '14 at 11:00
  • I tried this also Kanwaljit but after being stop it doesn't play next time.. its a big issue i need to play the music and stop the music n.. number of times means according my button click .. – Abhi Feb 28 '14 at 11:04
  • Kunwaljit i have tried Soundpool also but it supports sound clip upto 5sec only not more than that and my soundclips are 19 sec long\ – Abhi Feb 28 '14 at 11:06