Question

Media Player is starting and playing stream fine. But if I press stop once, I cannot play that again. App is closed. Code is here

Button StartStop;
final MediaPlayer MyRadio = new MediaPlayer();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StartStop = (Button) findViewById(R.id.StartStop);

    StartStop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if (MyRadio.isPlaying()){
                MyRadio.stop();
                MyRadio.release();
                StartStop.setText("Start");
            } else {
                PrepareRadio("http://220.247.162.146:7170/");
                MyRadio.start();
                StartStop.setText("Stop");
            }

        }
    });

}

public void PrepareRadio(String Source){
    try {
        MyRadio.reset();
        MyRadio.setDataSource(Source);
        MyRadio.prepare();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I need your advice to solve this.

Was it helpful?

Solution

Initialize your MediaPlayer class on onCreate() method.

 final MediaPlayer MyRadio;


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

  MyRadio = new MediaPlayer();

 }

OTHER TIPS

Move MyRadio.release(); and put it in onDestroy in your Activity

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top