我正在创建一个简单的在线音频流应用程序。它有4个按钮:播放、停止、继续、暂停。当我按下停止按钮时,它能正常工作,但是当我再次启动后,应用程序崩溃了。它会出现以下异常:
java.lang.IllegalStateException
at android.media.MediaPlayer._setAudioStreamType(Native Method)
at android.media.MediaPlayer.setAudioStreamType(MediaPlayer.java:1723)
at com.onlinestreaming.MediaPlayerActivity.onClick(MediaPlayerActivity.java:48)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
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)
这是我所做的:
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.play:
pd = new ProgressDialog(this);
pd.setMessage("Buffering");
pd.show();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnErrorListener(this);
try {
// mp.setDataSource("http://www.robtowns.com/music/blind_willie.mp3");
//mp.setDataSource("http://picosong.com/zkWc");
mp.setDataSource("http://songs1.djmazadownload.com/music/indian_movies/Banjo%20%282016%29/01%20-%20Bappa%20-%20Banjo%20%5BDJMaza.Cool%5D.mp3");
} catch (IOException e) {
Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
mp.prepareAsync();
mp.setOnCompletionListener(this);
break;
case R.id.Stop:
mp.stop();
mp.reset();
mp.release();
break;
case R.id.Resume:
mp.start();
break;
case R.id.Pause:
Toast.makeText(this, "Hit", Toast.LENGTH_SHORT).show();
if (mp.isPlaying()) {
mp.pause();
} else {
mp.start();
}
// mp.pause();
break;
}
}
@Override
public void onCompletion(MediaPlayer mp) {
pd.dismiss();
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
pd.dismiss();
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
Toast.makeText(this, "Prepared Finished", Toast.LENGTH_SHORT).show();
pd.setMessage("Playing.......");
mp.start();
}