gpt4 book ai didi

android - mediaPlayer第二次开始在线播放时出现IllegalStateException

转载 作者:太空狗 更新时间:2023-10-29 13:12:11 26 4
gpt4 key购买 nike

我正在创建一个简单的在线音频流应用程序。它有 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();
}

最佳答案

按下停止按钮时,您将释放 MediaPlayer 对象的资源。

发布():

Releases resources associated with this MediaPlayer object. It is considered good practice to call this method when you're done using the MediaPlayer. In particular, whenever an Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called), this method should be invoked to release the MediaPlayer object, unless the application has a special need to keep the object around. In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a MediaPlayer object is no longer needed may also lead to continuous battery consumption for mobile devices, and playback failure for other applications if no multiple instances of the same codec are supported on a device. Even if multiple instances of the same codec are supported, some performance degradation may be expected when unnecessary multiple instances are used at the same time.

因此从停止按钮点击监听器中移除 mp.release()。

 case R.id.Stop:
mp.stop();
mp.reset();
break;

关于android - mediaPlayer第二次开始在线播放时出现IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39117561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com