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卡連接到手機。 我不明白是什麼問題。給我任何提示或參考。
謝謝。
你有清單中的sdcard寫權限 ?它看起來像你的日誌的第一行顯示「SD卡訪問錯誤」? –
2012-08-01 07:00:48