录制音频和 java.lang.NullPointerException 错误

Record audio and java.lang.NullPointerException error

我正在开发一个应用程序,每当您单击录制我的音频的开始录制按钮时,当我单击停止录制按钮时,音频录制就会停止并将其保存在 sdcard,myFolder 中 当我点击开始录制时出现 java.lang.NullPointerException 错误,应用程序意外停止。我该如何解决? 请帮助我,我是一名新 android 开发人员。

public class MainActivity extends AppCompatActivity {
        protected static final int request = 0;

        Intent intent;
        private MediaRecorder myRecorder;
        Button btnStopRecord;
        Button btnStarRecord;
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final File dir = new File(Environment.getExternalStorageDirectory() + "/myFolder");
        dir.mkdirs();
        myRecorder = new MediaRecorder();
        myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        btnStarRecord = (Button) findViewById(R.id.StartRecording);
        btnStarRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Date date = new Date();
                final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
                myRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/myFolder" + "/" + dateFormat.format(date) + ".amr");
                start(v);
            }
        });
        btnStopRecord = (Button) findViewById(R.id.stopRecording);
        btnStopRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stop(v);
            }


        });
    }
private void stop(View v) {
        try {
            myRecorder.stop();
            myRecorder.release();
            myRecorder = null;

            btnStopRecord.setEnabled(false);
            btnStarRecord.setEnabled(true);

            Toast.makeText(getApplicationContext(), "Stop recording...",
                    Toast.LENGTH_SHORT).show();
        } catch (IllegalStateException e) {
            //  it is called before start()
            e.printStackTrace();
        } catch (RuntimeException e) {
            // no valid audio/video data has been received
            e.printStackTrace();
        }

    }


    private void start(View view) {
        try {
            myRecorder.prepare();
            myRecorder.start();

        } catch (IllegalStateException e) {
            // start:it is called before prepare()
            // prepare: it is called after start() or before setOutputFormat()
            e.printStackTrace();
        } catch (IOException e) {
            // prepare() fails
            e.printStackTrace();
        }}

08-25 13:59:54.036 21607-21607/com.example.mehrdad.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at com.example.mehrdad.myapplication.MainActivity.onClick(MainActivity.java:165) at android.view.View.performClick(View.java:2408) at android.view.View$PerformClick.run(View.java:8816) at android.os.Handler.handleCallback(Handler.java:587) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4633) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method)

将 myRecorder 初始化移动到 onClick 方法中。

btnStarRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Date date = new Date();
            final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");

            myRecorder = new MediaRecorder();
            myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);

            myRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/myFolder" + "/" + dateFormat.format(date) + ".amr");
            start(v);
        }
    });

当您调用停止时,您正在将记录器设置为空

myRecorder = null;

然后,如果您再次调用 "start",您会假设您的 myRecorder 已实例化,但事实并非如此。

我的建议是在需要开始录制时重新创建对象

编辑:

使用

创建方法
    myRecorder = new MediaRecorder();
    myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);

并在再次调用 start 之前调用它

从 onCreateView 中删除代码并检查此 link 并更新 permission.and 您不需要 myRecorder =null;在停止录制。

public void startRecording(){
            final Date date = new Date();
            final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
            myRecorder = new MediaRecorder();
            myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
            myRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/myFolder" + "/" + dateFormat.format(date) + ".amr");
            try {
        myRecorder.prepare();
        myRecorder.start();

    } catch (IllegalStateException e) {
        // start:it is called before prepare()
        // prepare: it is called after start() or before setOutputFormat()
        e.printStackTrace();
    } catch (IOException e) {
        // prepare() fails
        e.printStackTrace();
    }

    }