java.lang.IllegalStateException in MediaPlayer.isplaying() method

前端 未结 3 1321
夕颜
夕颜 2021-02-12 10:30
public static MediaPlayer mp=null;
public static void playGeneric(int name, final ImageButton button,final ImageButton pervious,Context context) {
    button.setEnabled(         


        
相关标签:
3条回答
  • 2021-02-12 11:16

    As android docs suggest that if mp is if has not been initialized at that time java.lang.IllegalStateException will be thrown so you have to initilize first or you have to write

    check out the docs http://developer.android.com/reference/android/media/MediaPlayer.html#isPlaying()

    try like this

     mp=MediaPlayer.create(context, name);
    
         try {
    
        if (mp.isPlaying()) {
            mp.stop();
            mp.release();
            mp=MediaPlayer.create(context, name);
        }
    
    
    
        mp.start();
    } catch (Exception e) {
    }
    
    0 讨论(0)
  • 2021-02-12 11:24

    Try changing mp.release() into reset(). that could help you.

    0 讨论(0)
  • 2021-02-12 11:30

    use runOnUiThread for mediaRecorder prepare.

    private boolean prepareMediaRecorder() {
    
        mediaRecorder = new MediaRecorder();
    
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
    
                mediaRecorder.reset();
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    
                mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
    
                mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    
                mediaRecorder.setOutputFile(filePath);
    
                try {
                    mediaRecorder.prepare();
                } catch (IOException e) {
                    mediaRecorder = null;
                    return;
                }
                mediaRecorder.start();
                recording = true;
            }
        });
    
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题