0

我正在创建一个录音应用程序。我需要录制音频并将其存储在 SD
卡中。但是当我尝试运行应用程序时它运行良好但是当我按下停止按钮时它显示消息“不幸的是你的应用程序已停止”
这是 logcat

enter code here  
08-01 12:09:18.331: E/SoundRecordingActivity(5611): sdcard access error
08-01 12:09:21.043: D/AndroidRuntime(5611): Shutting down VM
08-01 12:09:21.043: W/dalvikvm(5611): threadid=1: thread exiting with uncaught exception (group=0x2c3381f8)
08-01 12:09:21.043: E/AndroidRuntime(5611): FATAL EXCEPTION: main
08-01 12:09:21.043: E/AndroidRuntime(5611): java.lang.NullPointerException
08-01 12:09:21.043: E/AndroidRuntime(5611):     at com.blitze.recordingapp.SoundRecordingActivity$2.onClick(SoundRecordingActivity.java:110)
08-01 12:09:21.043: E/AndroidRuntime(5611):     at android.view.View.performClick(View.java:3511)
08-01 12:09:21.043: E/AndroidRuntime(5611):     at android.view.View$PerformClick.run(View.java:14115)
08-01 12:09:21.043: E/AndroidRuntime(5611):     at android.os.Handler.handleCallback(Handler.java:605)
08-01 12:09:21.043: E/AndroidRuntime(5611):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 12:09:21.043: E/AndroidRuntime(5611):     at android.os.Looper.loop(Looper.java:137)
08-01 12:09:21.043: E/AndroidRuntime(5611):     at android.app.ActivityThread.main(ActivityThread.java:4424)
08-01 12:09:21.043: E/AndroidRuntime(5611):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 12:09:21.043: E/AndroidRuntime(5611):     at java.lang.reflect.Method.invoke(Method.java:511)
08-01 12:09:21.043: E/AndroidRuntime(5611):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-01 12:09:21.043: E/AndroidRuntime(5611):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-01 12:09:21.043: E/AndroidRuntime(5611):     at dalvik.system.NativeStart.main(Native Method)
08-01 12:09:23.759: W/System.err(5611): java.lang.Throwable: stack dump
08-01 12:09:23.759: W/System.err(5611):     at java.lang.Thread.dumpStack(Thread.java:496)
08-01 12:09:23.759: W/System.err(5611):     at android.os.Process.killProcess(Process.java:725)
08-01 12:09:23.759: W/System.err(5611):     at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:82)
08-01 12:09:23.759: W/System.err(5611):     at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
08-01 12:09:23.759: W/System.err(5611):     at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
08-01 12:09:23.759: W/System.err(5611):     at dalvik.system.NativeStart.main(Native Method)  

这是我的代码

startButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            startButton.setEnabled(false);
            stopButton.setEnabled(true);

            File sampleDir = Environment.getExternalStorageDirectory();
            try {
                audiofile = File.createTempFile("sound", ".3gp", sampleDir);

            recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(audiofile.getAbsolutePath());
            recorder.prepare();
            recorder.start();




            } catch (IOException e) {
                Log.e(TAG, "sdcard access error");
                return;
            }



        }
    });
    stopButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            startButton.setEnabled(true);
            stopButton.setEnabled(false);
            recorder.stop();
            recorder.release();

            //counter.cancel();
            addRecordingToMediaLibrary();

        }
    });

protected void addRecordingToMediaLibrary() {
    ContentValues values = new ContentValues(4);
    long current = System.currentTimeMillis();
    values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
    values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
    values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
    values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
    ContentResolver contentResolver = getContentResolver();

    Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Uri newUri = contentResolver.insert(base, values);

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
    Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
}

}

我将 SD 卡连接到手机。我不明白有什么问题。给我任何提示或参考。
谢谢..

4

1 回答 1

0

您需要查看第 110 行,它是 OnClick 函数中的一行。您将引用“recorder.stop()”之类的内容,此时的记录器将为 NULL。

看起来您需要将在 start 函数中创建的记录器对象的实例传递给 stop 函数。

于 2012-08-01T07:04:44.497 回答