2

I am developing a simple live audio streaming app. The app runs but illegal state exception occurs after mediaplayer.preapre(). Can anyone help what am I doing wrong?

public class MainActivity extends Activity {

MediaPlayer mPlayer;
Button buttonPlay;
Button buttonStop;
String url = "http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    buttonPlay = (Button) findViewById(R.id.play);
    buttonPlay.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mPlayer = new MediaPlayer();
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

            try {
                mPlayer.setDataSource(url);
            } catch (IllegalArgumentException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly! IllegalArgumentException", Toast.LENGTH_LONG).show();
            } catch (SecurityException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly!SecurityException", Toast.LENGTH_LONG).show();
            } catch (IllegalStateException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly!IllegalStateException", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                mPlayer.prepare();
            } catch (IllegalStateException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            }

            mPlayer.start();
        }
    });

    buttonStop = (Button) findViewById(R.id.stop);
    buttonStop.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(mPlayer!=null && mPlayer.isPlaying()){
                mPlayer.stop();
            }
        }
    });

}

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

Following messages appear in logcat:

E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present
E/MediaPlayer: prepareAsync called in state 1 E/MediaPlayer: Unable to create media player
E/MediaPlayer: prepareAsync called in state 1
E/MediaPlayer: start called in state 1
E/MediaPlayer: error (-38, 0)
Noor
  • 193
  • 1
  • 2
  • 15
  • Possible duplicate of [Media Player called in state 0, error (-38,0)](http://stackoverflow.com/questions/9008770/media-player-called-in-state-0-error-38-0) – Viktor Yakunin Nov 14 '16 at 11:00

0 Answers0