simerpeet jassal simerpeet jassal - 2 months ago 20
Android Question

java.lang.IllegalStateException for media player

Hi i am building online radio streaming app. i am getting java.lang.IllegalStateException error when i am pressing play button. Main activity looks like following .

package com.radioawaz.simerpreetjassal.radioawaz;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

import java.io.IOException;

public class MainActivity extends Activity implements OnClickListener {

private ProgressBar playSeekBar;

private Button buttonPlay;

private Button buttonStopPlay;

private MediaPlayer player;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initializeUIElements();

initializeMediaPlayer();
}

private void initializeUIElements() {

playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);

buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);

buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);

}

public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}

private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);

playSeekBar.setVisibility(View.VISIBLE);

player.prepareAsync();

player.setOnPreparedListener(new OnPreparedListener() {

public void onPrepared(MediaPlayer mp) {
player.start();
}
});

}

private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}

buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}

private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://streaming.shoutcast.com/Radioawazca?lang=en-ca");
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}

@Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}


Erorr Log cat

09-29 20:51:51.278 7852-7852/com.radioawaz.simerpreetjassal.radioawaz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.radioawaz.simerpreetjassal.radioawaz, PID: 7852
java.lang.IllegalStateException
at android.media.MediaPlayer.prepareAsync(Native Method)
at com.radioawaz.simerpreetjassal.radioawaz.MainActivity.startPlaying(MainActivity.java:67)
at com.radioawaz.simerpreetjassal.radioawaz.MainActivity.onClick(MainActivity.java:55)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-29 20:51:52.978 7852-7852/com.radioawaz.simerpreetjassal.radioawaz I/Process: Sending signal. PID: 7852 SIG: 9


Hi i am building online radio streaming app. i am getting java.lang.IllegalStateException error when i am pressing play button. Main activity looks like following .

Answer

Please add <uses-permission android:name="android.permission.INTERNET"/> in your AndroidManifest.xml file.