Android音频开发MediaPlayer(-38, 0)错误解决

这个问题是出现在,切换音乐播放的时候,出现-38的问题,问题的原因应该是MediaPlayer没有初始化造成的吧,这篇文章给的一个思路是,切换音乐播放的时候,销毁掉之前的MediaPlayer对象,然后重现创建一个新的MediaPlayer对象

https://blog.csdn.net/SolarLove/article/details/80745147

public void playPrepareAsync() {
        try {
            if (mUrl == null) {
                return;
            }
            destoryMediaPlayer();
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnSeekCompleteListener(this);
            mMediaPlayer.setOnErrorListener(this);
            mMediaPlayer.setOnBufferingUpdateListener(this);
           if (mUrl.startsWith("android.resource://")) {
                mMediaPlayer.setDataSource(this, Uri.parse(mUrl));
            } else {
                mMediaPlayer.setDataSource(mUrl);
            }
            mMediaPlayer.prepareAsync();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            Log.e(TAG, "IllegalArgumentException无法播放" + mUrl);
        } catch (IOException e) {
            Log.e(TAG, "IOException无法播放" + mUrl);
            e.printStackTrace();
        } catch (Exception e) {
            Log.e(TAG, "Exception无法播放" + mUrl);
            setErrPlayState("" + "播放失败 " + e.toString());
        }
    }
 
public void destoryMediaPlayer() {
        try {
            if (mediaPlayer != null) {
                mediaPlayer.setOnCompletionListener(null);
                mediaPlayer.setOnPreparedListener(null);
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
            }
        } catch (Exception e) {
        }
    }

你可能感兴趣的:(Android音频开发MediaPlayer(-38, 0)错误解决)