I am making a simple app to play a online radio stream. I know how to set up the stream. Here is my code so far. It sets up the stream and plays it fine. But if I hit stop, I cannot get it to start again. Am I missing something obvious?
final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(url);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // might take long! (for buffering, etc)
streamButton = (ToggleButton) findViewById(R.id.playstopstream);
streamButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, final boolean isChecked) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if(isChecked)
{
mediaPlayer.start();
}
else
{
mediaPlayer.stop();
mediaPlayer.reset();
}
}
Old question, but I came across it looking for this. Just use pause to stop, followed by seekTo(0) before restarting:
mediaPlayer.seekTo(0);
mediaPlayer.start();
Works without having to prepare the recorder again every time.
When you call mediaPlayer.reset()
mediaPlayer
goes to IDLE state.
For playback, you should do initialize to prepare before start.
You can check the mediaplayer state here:
http://developer.android.com/reference/android/media/MediaPlayer.html.
public void playButton (View view){
if (isActive == false){
mediaPlayer.start();
isActive = true;
}else{
mediaPlayer.seekTo(0);
mediaPlayer.start();
}
}
Works OK with me.
Try this
@Override
public void onCheckedChanged(CompoundButton arg0, final boolean isChecked) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if(isChecked) {
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
}
else {
mediaPlayer.stop();
mediaPlayer.reset();
}
}
You must be call onDestroy() medthod when finish you song / play song.
You get help from my real time code.
public class RingtonePlayingService extends Service {
public static MediaPlayer mediaPlayer;
public static Context context;
@SuppressWarnings("deprecation")
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mediaPlayer = new MediaPlayer();
// this is important.
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
try {
// Uri ringerUri = RingtoneManager.getActualDefaultRingtoneUri(
// getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
mediaPlayer.setDataSource(context,
Settings.System.DEFAULT_RINGTONE_URI);
// mediaPlayer.setDataSource(context, ringerUri);
mediaPlayer.setLooping(true);
mediaPlayer.prepare();
if (SettingsManager.getInstance(context).getFinishCall()) {
} else {
mediaPlayer.start();
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
TTcall.onDestroyr();
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
mediaPlayer.stop();
mediaPlayer = null;
}
}
}
}
I expected you will be helpful from above code. please try using like this. Thanks
Also you likely don't need to stop()
, pause()
would likely suffice, meaning that you can call play()
once you want to continue playing.
mediaPlay.seek(0);
mediaPlay.pause();