Media Player正在启动,并且可以正常播放。但是,如果我按一下Stop键,则无法再次播放。应用已关闭。代码在这里
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();
}
}
我需要您的建议来解决此问题。
最佳答案
在MediaPlayer
方法上初始化您的onCreate()
类。
final MediaPlayer MyRadio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyRadio = new MediaPlayer();
}