媒体记录器在尝试在LG G Watch上录制音频时抛出"java.lang.RuntimeException: start failed: -2147483648"



我正试图在LG G手表上的应用程序中录制音频。以下代码在语句"recorder.start();"处引发RuntimeException,并显示消息"start failed:-2147483648"。

我尝试了很多不同的参数集,例如AudioSource:

recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
//-and-
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

对于OutputFormat,我也尝试过

recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
//-and-
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//-and-
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

还尝试了不同的AudioEncoder

recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
//-and-
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

这是源代码:

            final MediaRecorder recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            //recorder.setAudioSamplingRate(8000);  // tried with and without this line
            String filename = mMainActivity.getExternalFilesDir(null) + "/my_recording.mp4"; // changed to corresponding extension name for the output format
            Log.d("DEBUG", "Output filename = " + filename);
            recorder.setOutputFile(filename);
            try {
                recorder.prepare();
            }
            catch (IOException e) {
                Log.d("DEBUG", "IOException while prepare(): " + e.getMessage());
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Log.d("DEBUG", "InterruptedException while Thread.sleep(): " + e.getMessage());
            }
            Log.d("DEBUG", "before start");
            try {
                recorder.start(); // <--- exception is thrown here
            }
            catch (IllegalStateException e) {
                Log.d("DEBUG", "IllegalStateException while start(): " + e.getMessage());
            }
            Log.d("DEBUG", "after start");

------------编辑--------------------

以下是logcat输出:

09-22 01:33:10.697    8764-8764/com.company.project D/DEBUG﹕ Output filename = /storage/emulated/0/Android/data/com.company.project/files/my_recording.mp4
09-22 01:33:11.697    8764-8764/com.company.project D/DEBUG﹕ before start
09-22 01:33:11.707    8764-8764/com.company.project E/MediaRecorder﹕ start failed: -2147483648
09-22 01:33:11.707    8764-8764/com.company.project D/AndroidRuntime﹕ Shutting down VM
09-22 01:33:11.707    8764-8764/com.company.project W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xada14d70)
09-22 01:33:11.737    8764-8764/com.company.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.company.project, PID: 8764
java.lang.RuntimeException: start failed.
        at android.media.MediaRecorder.start(Native Method)
        at com.company.project.MainActivity$1.onLayoutInflated(MainActivity.java:56)
        at android.support.wearable.view.WatchViewStub.inflate(WatchViewStub.java:133)
        at android.support.wearable.view.WatchViewStub.onMeasure(WatchViewStub.java:141)
        at android.view.View.measure(View.java:16648)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16648)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2338)
        at android.view.View.measure(View.java:16648)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1959)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1145)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1396)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1032)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5657)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5026)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
        at dalvik.system.NativeStart.main(Native Method)
09-22 01:33:11.777    8764-8764/com.company.project I/Process﹕ Sending signal. PID: 8764 SIG: 9

注意:我在清单中有以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

我的环境是:

SDK: API 20: Android 4.4 (KitKat Wear)
Build tool: 19.1.0
Android Studio: 0.8.9

您不能在Android Wear中使用MediaRecorder,只能使用AudioRecord。这是一段YouTube视频,谷歌开发者在视频中谈论他们如何录制语音识别音频:

https://www.youtube.com/watch?v=sha_w3_5c2c#t=1970

最新更新