Android Uncaught Handler:线程主要由于未捕获的异常而退出

时间:2011-11-15 18:58:41

标签: android

我在使用AudioRecord课时遇到问题。我已经创建了几个基本的应用程序,但无法弄清楚我在录制音频时遇到了什么问题。首先,我得到了一个“未被捕获的处理程序”,所以甚至没有任何东西可以启动。然后,我不确定录制声音需要什么,停止并播放它。那是什么.start,.stop都做了,我该如何使用它。

package com.dustin.audioRecord;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
//import android.media.MediaRecorder.AudioSource;
//import android.media.MediaRecorder.AudioEncoder;

public class AudioRecordActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button record_button = (Button)findViewById(R.id.record_button);
    final Button stop_button = (Button)findViewById(R.id.stop_button);
    //final Button play_button = (Button)findViewById(R.id.play_button);
    //int audioSource = 0;
    int sampleRateInHz = 44100; 
    int channelConfig = AudioFormat.CHANNEL_IN_MONO; 
    int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    final int bufferSizeInBytes = AudioRecord.getMinBufferSize( sampleRateInHz, 
                                                            channelConfig, 
                                                            audioFormat);


    //final MediaRecorder recorder = new MediaRecorder();
    final AudioRecord record = new AudioRecord( MediaRecorder.AudioSource.MIC, 
                                                sampleRateInHz, 
                                                audioFormat, 
                                                channelConfig,
                                                bufferSizeInBytes);

    //final byte[] tempBuffer = new byte[bufferSizeInBytes];

    record_button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            record.getAudioFormat();
            record.startRecording();

            //int buffer = record.read(tempBuffer, 0, bufferSizeInBytes);

        }
    });

    stop_button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            record.stop();
            record.release();
        }
    });


}
//public static final int RECORDSTATE_RECORDING = 3;
//public static final int RECORDSTATE_STOPPED = 1;
//public static final int RECORDSTATE_INITIALIZED = 1;
//public static final int MIC = 1;
//public static final int RECORDSTATE_UNINITIALIZED = 0;

}

这是logcat读出来的:

11-15 11:36:25.498: W/dalvikvm(349): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
11-15 11:36:25.498: E/AndroidRuntime(349): Uncaught handler: thread main exiting due to uncaught exception
11-15 11:36:25.558: E/AndroidRuntime(349): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dustin.audioRecord/com.dustin.audioRecord.AudioRecordActivity}: java.lang.IllegalArgumentException: Unsupported sample encoding. Should be ENCODING_PCM_8BIT or ENCODING_PCM_16BIT.
11-15 11:36:25.558: E/AndroidRuntime(349):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-15 11:36:25.558: E/AndroidRuntime(349):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-15 11:36:25.558: E/AndroidRuntime(349):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-15 11:36:25.558: E/AndroidRuntime(349):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-15 11:36:25.558: E/AndroidRuntime(349):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 11:36:25.558: E/AndroidRuntime(349):  at android.os.Looper.loop(Looper.java:123)
11-15 11:36:25.558: E/AndroidRuntime(349):  at android.app.ActivityThread.main(ActivityThread.java:4363)
11-15 11:36:25.558: E/AndroidRuntime(349):  at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:36:25.558: E/AndroidRuntime(349):  at java.lang.reflect.Method.invoke(Method.java:521)
11-15 11:36:25.558: E/AndroidRuntime(349):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-15 11:36:25.558: E/AndroidRuntime(349):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-15 11:36:25.558: E/AndroidRuntime(349):  at dalvik.system.NativeStart.main(Native Method)
11-15 11:36:25.558: E/AndroidRuntime(349): Caused by: java.lang.IllegalArgumentException: Unsupported sample encoding. Should be ENCODING_PCM_8BIT or ENCODING_PCM_16BIT.
11-15 11:36:25.558: E/AndroidRuntime(349):  at android.media.AudioRecord.audioParamCheck(AudioRecord.java:305)
11-15 11:36:25.558: E/AndroidRuntime(349):  at android.media.AudioRecord.<init>(AudioRecord.java:223)
11-15 11:36:25.558: E/AndroidRuntime(349):  at com.dustin.audioRecord.AudioRecordActivity.onCreate(AudioRecordActivity.java:33)
11-15 11:36:25.558: E/AndroidRuntime(349):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-15 11:36:25.558: E/AndroidRuntime(349):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-15 11:36:25.558: E/AndroidRuntime(349):  ... 11 more
11-15 11:36:25.578: I/dalvikvm(349): threadid=7: reacting to signal 3
11-15 11:36:25.598: I/dalvikvm(349): Wrote stack trace to '/data/anr/traces.txt'

1 个答案:

答案 0 :(得分:0)

看看你的异常:

java.lang.IllegalArgumentException: Unsupported sample encoding. Should be ENCODING_PCM_8BIT or ENCODING_PCM_16BIT

声音文件的编码错误。修复声音文件。