Hello Friends,
I make one app in android, in which i take three radio button in radiogroup and take two buttons “play” and “stop”.
my problem is that when i play the song it is playing well but when i click on stop button and then click on play button then song is not playing,
how to do that ? please help me if anybody know.
below is code that i write:
package com.mediaplayer;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MediaplayerActivity extends Activity implements OnCheckedChangeListener {
MediaPlayer song1,song2,song3;
int whatsong = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioGroup rgMusic = (RadioGroup) findViewById(R.id.radioGroup1);
song1 = MediaPlayer.create(MediaplayerActivity.this, R.raw.fluet);
song2 = MediaPlayer.create(MediaplayerActivity.this, R.raw.mogra_na_phool);
song3 = MediaPlayer.create(MediaplayerActivity.this, R.raw.airtel);
Button bPlay = (Button) findViewById(R.id.bPlay);
Button bStop = (Button) findViewById(R.id.bStop);
rgMusic.setOnCheckedChangeListener(this);
bPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(song1.isPlaying())
{
song1.pause();
song1.seekTo(0);
}
if(song2.isPlaying())
{
song2.pause();
song2.seekTo(0);
}
if(song3.isPlaying())
{
song3.pause();
song3.seekTo(0);
}
switch (whatsong) {
case 1:
try {
song1.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
song1.start();
break;
case 2:
try {
song2.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
song2.start();
break;
case 3:
try {
song3.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
song3.start();
break;
}
}
});
bStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (whatsong) {
case 1:
song1.stop();
break;
case 2:
song2.stop();
break;
case 3:
song3.stop();
break;
}
}
});
}
@Override
public void onCheckedChanged(RadioGroup group, int rbId) {
switch (rbId) {
case R.id.rbMusic1:
whatsong = 1;
break;
case R.id.rbMusic2:
whatsong = 2;
break;
case R.id.rbMusic3:
whatsong = 3;
break;
}
}
}
now use this edited code. This code will help you. If it is not running properly inform me.