使用MediaRecorder时出现奇怪的RuntimeException

时间:2014-03-05 17:31:29

标签: android mediarecorder android-mediarecorder

我的代码如下:

private void startRecording() {
            try {

                mFileName = getActivity().getDir("testDir", MODE_PRIVATE).getAbsolutePath();
                mFileName += "/audiorecordtest.3gp";
                Log.i(LOG_TAG, mFileName);
                //FileOutputStream fos = getActivity().getApplicationContext().openFileOutput(mFileName, Context.MODE_PRIVATE);

                mRecorder = new MediaRecorder();
                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                mRecorder.setOutputFile(mFileName);
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
                mRecorder.setAudioEncodingBitRate(16);
                mRecorder.setAudioSamplingRate(44100);
                mRecorder.prepare();
            } catch (IOException e) {
                Log.e(LOG_TAG, "prepare() failed" + e.getLocalizedMessage());
            } catch(RuntimeException re){
                Log.e(LOG_TAG, "runtime exception" + re.getLocalizedMessage());
            }
            mRecorder.start();
        }

当我尝试在模拟器(或我的手机)上运行应用程序时,应用程序崩溃,我得到以下LogCat日志:

03-05 12:14:46.770: I/AudioRecordTest(5035): /data/data/com.example.test/app_testDir/audiorecordtest.3gp
03-05 12:14:46.890: E/MediaRecorder(5035): start failed: -2147483648
03-05 12:14:46.890: D/AndroidRuntime(5035): Shutting down VM
03-05 12:14:46.900: W/dalvikvm(5035): threadid=1: thread exiting with uncaught exception (group=0xb1a2aba8)
03-05 12:14:46.960: E/AndroidRuntime(5035): FATAL EXCEPTION: main
03-05 12:14:46.960: E/AndroidRuntime(5035): Process: com.example.test, PID: 5035
03-05 12:14:46.960: E/AndroidRuntime(5035): java.lang.RuntimeException: start failed.
03-05 12:14:46.960: E/AndroidRuntime(5035):     at android.media.MediaRecorder.start(Native Method)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at com.example.test.MainActivity$RecordPlayFragment.startRecording(MainActivity.java:331)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at com.example.test.MainActivity$RecordPlayFragment.onRecord(MainActivity.java:277)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at com.example.test.MainActivity$RecordPlayFragment.access$0(MainActivity.java:275)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at com.example.test.MainActivity$RecordPlayFragment$RecordButton$1.onClick(MainActivity.java:345)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at android.view.View.performClick(View.java:4438)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at android.view.View$PerformClick.run(View.java:18422)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at android.os.Handler.handleCallback(Handler.java:733)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at android.os.Looper.loop(Looper.java:136)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at java.lang.reflect.Method.invokeNative(Native Method)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at java.lang.reflect.Method.invoke(Method.java:515)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-05 12:14:46.960: E/AndroidRuntime(5035):     at dalvik.system.NativeStart.main(Native Method)

我只是想知道是否有人知道这里发生了什么,因为谷歌搜索后我没有发现任何看起来像是同样的错误。

1 个答案:

答案 0 :(得分:0)

添加于:

File tempFile = new File(mFileName);

并将mediaRecorder.setOutputFile更改为:

mRecorder.setOutputFile(tempFile.getPath());

甚至不确定它为什么不能使用文件流,但它可以这样工作。