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

标签 android android-mediaplayer

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

相关文章:

java - 将两个数组放在一起

android - 白日梦权限请求

java - 使用jar中的资源

Android 音频流太慢

android - 音频焦点更改不起作用

Android:依次播放两种声音

java - 在 TextView 中的两个可绘制对象之间绘制线条

android - 如何避免由于自定义静态处理程序类导致的内存泄漏?

android - 如何获取音频流的持续时间并从任何点继续音频流

java - Android MediaPlayer 暂停