NullPointerException when recording is stopped android

杀马特。学长 韩版系。学妹 提交于 2020-01-15 06:01:12

问题


I'm trying to make an app that starts recording when a button is clicked, and when is clicked again it stops recording. I don't think I have problems when I'm recording, but when I click the button again the application crashes with NullPointerException and it is said that stop is called in invalid state 1

My code:

package com.example.victwo;

import java.io.IOException;

import android.media.MediaRecorder;

public class Audio {
    private String path;

    MediaRecorder myAudioRecorder = null;

    public Audio(String path) {
        this.path = path;
    }

    public void play() {

    }

    public void startRecording() {
        myAudioRecorder = new MediaRecorder();
        myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myAudioRecorder.setOutputFile(path);

        try {
            myAudioRecorder.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        myAudioRecorder.start();
    }

    public void stopRecording() {
        myAudioRecorder.stop();
        myAudioRecorder.release();
        myAudioRecorder = null;
    }

}

This is the code from the activity where I call the Audio Class methods:

    case R.id.action_record:
        Audio recAndPlay = new Audio(this.path + "/" + this.title);

        if(flag == 0) {
            Log.d("RECORD", "START");
            flag = 1;
            if(title.equals("")) {
                setTitle();
            }
            recAndPlay.startRecording();
            return true;
        }

        if(flag == 1) {
            Log.d("RECORD", "STOP");
            recAndPlay.stopRecording();
        }
        return true;

The logcat:

04-05 14:02:58.909: E/AndroidRuntime(5730): FATAL EXCEPTION: main
04-05 14:02:58.909: E/AndroidRuntime(5730): java.lang.NullPointerException
04-05 14:02:58.909: E/AndroidRuntime(5730):     at com.example.victwo.Audio.stopRecording(Audio.java:41)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at com.example.victwo.NoteActivity.onOptionsItemSelected(NoteActivity.java:158)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.app.Activity.onMenuItemSelected(Activity.java:2205)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.app.ActionBarActivity.superOnMenuItemSelected(ActionBarActivity.java:244)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:339)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:130)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.app.ActionBarActivityDelegateBase.onMenuItemSelected(ActionBarActivityDelegateBase.java:344)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:777)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:922)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:544)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:105)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.view.View.performClick(View.java:2485)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.view.View$PerformClick.run(View.java:9080)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.os.Handler.handleCallback(Handler.java:587)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.os.Looper.loop(Looper.java:123)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at java.lang.reflect.Method.invokeNative(Native Method)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at java.lang.reflect.Method.invoke(Method.java:507)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-05 14:02:58.909: E/AndroidRuntime(5730):     at dalvik.system.NativeStart.main(Native Method)

回答1:


Initialize your media player in out side of startRecording()

 myAudioRecorder = new MediaRecorder();

Modify your code like following

public class Audio {
private String path;




MediaRecorder myAudioRecorder = new MediaRecorder();

public Audio(String path) {
    this.path = path;
}

public void play() {

}

public void startRecording() {

    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myAudioRecorder.setOutputFile(path);

    try {
        myAudioRecorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    myAudioRecorder.start();
}

public void stopRecording() {
    myAudioRecorder.stop();
    myAudioRecorder.release();
    myAudioRecorder = null;
}
}

Hope this will help you




回答2:


The myAudioRecorder must be null at the time you call stopRecording(). I can not judge from your code why (do you call stopRecording() twice?). If you post more code I might be able to help you. You could also surround the statements in the stopRecording()method with a check:

public void stopRecording() {
    if(myAudioRecorder != null){
       myAudioRecorder.stop();
       myAudioRecorder.release();
       myAudioRecorder = null;
    }
}



回答3:


private void stopRecording() {
        if (null != recorder) {
            recorder.stop();
            recorder.reset();
            recorder.release();

            recorder = null;
        }
    }



回答4:


please check if MediaRecorder object is not null before stopping it.




回答5:


I don't think you are taking the right approach. why declare media player as null in the beginning. initialise in first and when done use methods of media player class to release/reset it.

also before you start using mediaplayer use if(){...}block to check whether it is initialised. example below;

   private void stopRecording() {
      if (myAudioRecorder != null) {
        myAudioRecorder.stop();
        myAudioRecorder.reset();
        myAudioRecorder.release();

        myAudioRecorder = null;
     }
  }



回答6:


Another reason could be, that it was already stopped, and then you are re-using .stop() function, which causes problems, check your logic and code...



来源:https://stackoverflow.com/questions/22880037/nullpointerexception-when-recording-is-stopped-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!