Reputation: 520
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.
Upvotes: 1
Views: 157
Reputation: 18923
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();
}
Upvotes: 1