Android media player returns IllegalStateException

后端 未结 3 1695
北荒
北荒 2021-01-12 00:25

I have following code to play small audio files

private void playVoice() {

     if (mPlayVoice != null) {
         if (mPlayVoice.isPlaying()) {
                    


        
相关标签:
3条回答
  • 2021-01-12 01:05

    You're doing this:

     PlayVoice.release(); 
    

    Do you not mean

     mPlayVoice.release(); 
    

    If you have other issues this is the best document to consult:

    Android MediaPlayer

    EDIT

    Ok if you are here: isPlaying() Invalid States it show's you're trying to call isPlaying() while the player is in the error state. So you need to work out why it is already in the error state.

    In general, some playback control operation may fail due to various reasons, such as unsupported audio/video format, poorly interleaved audio/video, resolution too high, streaming timeout, and the like.

    Have a look at adding an error listener: setOnErrorListener()

    0 讨论(0)
  • 2021-01-12 01:12

    Use the following code as i was facing the same exception.

    try {
        if(mPlayVoice!=null && mPlayVoice.isPlaying()) {
            Log.d("TAG------->", "player is running");
            mPlayVoice.stop();
            Log.d("Tag------->", "player is stopped");
            mPlayVoice.release();
            Log.d("TAG------->", "player is released");
        }
    } catch(Exception e){
    }
    

    Here write whatever you want to do. Actually the condition checking like isPlaying() or checking for null generates the IllegalStateException.....

    0 讨论(0)
  • 2021-01-12 01:15

    You may have to clear the audioGroup joined with audioStream. Mine worked with the following code:

    public static void audioPlayCaptureStop()
            {
    
                try 
                {
                     if(audioStream.isBusy()) 
                     {
                         audioGroup.clear();
                         audioStream.release();
                         System.out.println("audioStream released");
                     }
    
                } catch (Exception e) {
                    System.out.println("audioStream release exception: "+e.toString());
                }
            }
    
    0 讨论(0)
提交回复
热议问题