录音中出现例外情况

时间:2012-08-01 06:57:11

标签: android exception sd-card audio-recording

我正在创建一个录制应用程序。我需要录制音频并将其存储在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卡连接到手机上。 我不明白是什么问题。给我任何提示或参考。
谢谢..

1 个答案:

答案 0 :(得分:0)

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

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