播放新歌时会收到IllegalStateException

时间:2017-08-05 21:26:11

标签: android compiler-errors android-mediaplayer

我正在开发一个音频播放器应用程序它工作正常但是当我发送一个新的arraylist它只是崩溃。我无法理解我在做什么错误 这是错误日志。

java.lang.RuntimeException: Error receiving broadcast Intent { act=beatbox.neelay.dummybeat.PlayNewAudio flg=0x10 } in unnion.neelay.beatbox.MediaService$6@a324e56
                                                                       at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:893)
                                                                       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:5441)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
                                                                    Caused by: java.lang.IllegalStateException
                                                                       at android.media.MediaPlayer._setAudioStreamType(Native Method)
                                                                       at android.media.MediaPlayer.setAudioStreamType(MediaPlayer.java:1732)
                                                                       at unnion.neelay.beatbox.MediaService.initMediaPlayer(MediaService.java:408)
                                                                       at unnion.neelay.beatbox.MediaService.access$1500(MediaService.java:52)
                                                                       at unnion.neelay.beatbox.MediaService$6.onReceive(MediaService.java:904)
                                                                       at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:883)
                                                                       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:5441) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) 
有人可以指导我如何解决。 这是它指示的方法

private void initMediaPlayer() {
    if (mediaPlayer == null)
        mediaPlayer = new MediaPlayer();//new MediaPlayer instance

    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    mediaPlayer.setOnPreparedListener(this);
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnSeekCompleteListener(this);
    mediaPlayer.setOnInfoListener(this);
    //Reset so that the MediaPlayer is not pointing to another data source



    StorageUtil active = new StorageUtil(getApplicationContext());
    active.storeActiveIndex(audioIndex);


    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        // Set the data source to the mediaFile location
        mediaPlayer.reset();
        mediaPlayer.setDataSource(activeAudio.getFile_uri());
    } catch (IOException e) {
        e.printStackTrace();
        stopSelf();
    }


    mediaPlayer.prepareAsync();
    messagesent();
}

1 个答案:

答案 0 :(得分:0)

避免在播放状态下调用setAudioStreamType。别忘了发布MediaPlayer。

private void initMediaPlayer() {
    if (mediaPlayer == null) {
        mediaPlayer = new MediaPlayer();//new MediaPlayer instance

        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnErrorListener(this);
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnSeekCompleteListener(this);
        mediaPlayer.setOnInfoListener(this);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }

    //Reset so that the MediaPlayer is not pointing to another data source

    StorageUtil active = new StorageUtil(getApplicationContext());
    active.storeActiveIndex(audioIndex);

    try {
        // Set the data source to the mediaFile location
        mediaPlayer.reset();
        mediaPlayer.setDataSource(activeAudio.getFile_uri());
    } catch (IOException e) {
        e.printStackTrace();
        stopSelf();
    }

    mediaPlayer.prepareAsync();
    messagesent();
}