如何在Android中一次录制多个音轨

时间:2014-04-15 03:57:25

标签: android audio illegalstateexception mediarecorder

我一直在研究一个需要能够同时从麦克风录制两个独立文件的应用程序,但是我不断收到非法状态异常并且我的应用程序崩溃了。

当我一次只记录一个代码时,代码工作正常,但我希望能够同时执行这两个代码。

活动代码

rec_sound_1 = (Button) findViewById(R.id.rec_sound_1);
        rec_sound_1.animate().setDuration(105);
        rec_sound_1.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {

                if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
                    //begin recording
                    try {
                        RecordingHandler.beginRecording1(SOUND_ONE);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    rec_sound_1.animate().setInterpolator(sDecelerator)
                            .scaleX(.85f).scaleY(.85f);
                } else if (arg1.getAction() == MotionEvent.ACTION_UP) {
                    rec_sound_1.animate().setInterpolator(sOvershooter)
                            .scaleX(1f).scaleY(1f);
                    //stop recording
                    RecordingHandler.stopRecording1();
                }
                return false;
            }
        });

        rec_sound_2 = (Button) findViewById(R.id.rec_sound_2);
        rec_sound_2.animate().setDuration(105);
        rec_sound_2.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {

                if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
                    //begin recording
                    try {
                        RecordingHandler.beginRecording2(SOUND_TWO);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    rec_sound_2.animate().setInterpolator(sDecelerator)
                            .scaleX(.85f).scaleY(.85f);
                } else if (arg1.getAction() == MotionEvent.ACTION_UP) {
                    rec_sound_2.animate().setInterpolator(sOvershooter)
                            .scaleX(1f).scaleY(1f);
                    //stop recording
                    RecordingHandler.stopRecording2();
                }
                return false;
            }
        });

        rec_sound_3 = (Button) findViewById(R.id.rec_sound_3);
        rec_sound_3.animate().setDuration(105);
        rec_sound_3.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {

                if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
                    //begin recording
                    try {
                        RecordingHandler.beginRecording3(SOUND_THREE);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    rec_sound_3.animate().setInterpolator(sDecelerator)
                            .scaleX(.85f).scaleY(.85f);
                } else if (arg1.getAction() == MotionEvent.ACTION_UP) {
                    rec_sound_3.animate().setInterpolator(sOvershooter)
                            .scaleX(1f).scaleY(1f);
                    //stop recording
                    RecordingHandler.stopRecording3();
                }
                return false;
            }
        });

        rec_sound_4 = (Button) findViewById(R.id.rec_sound_4);
        rec_sound_4.animate().setDuration(105);
        rec_sound_4.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {

                if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
                    //begin recording
                    try {
                        RecordingHandler.beginRecording4(SOUND_FOUR);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    rec_sound_4.animate().setInterpolator(sDecelerator)
                            .scaleX(.85f).scaleY(.85f);
                } else if (arg1.getAction() == MotionEvent.ACTION_UP) {
                    rec_sound_4.animate().setInterpolator(sOvershooter)
                            .scaleX(1f).scaleY(1f);
                    //stop recording
                    RecordingHandler.stopRecording4();
                }
                return false;
            }
        });

录制处理程序

public class RecordingHandler {

    private static MediaRecorder recorder1;
    private static MediaRecorder recorder2;
    private static MediaRecorder recorder3;
    private static MediaRecorder recorder4;

    static void beginRecording1(String output) throws Exception {
        ditchMediaRecorder1();
        File outFile = new File(output);        
        if(outFile.exists())
            outFile.delete();       
        recorder1 = new MediaRecorder();
        recorder1.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder1.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder1.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
        recorder1.setAudioEncodingBitRate(16);
        recorder1.setAudioSamplingRate(44100);
        recorder1.setOutputFile(output);
        recorder1.prepare();
        recorder1.start(); 
    }
    private static void ditchMediaRecorder1() {
        //if we have the recorder, release it
        if(recorder1 != null)
            recorder1.release(); }  
    static void stopRecording1() {
        if(recorder1 != null)
            recorder1.stop(); }

    static void beginRecording2(String output) throws Exception {
        ditchMediaRecorder2();
        File outFile = new File(output);        
        if(outFile.exists())
            outFile.delete();       
        recorder2 = new MediaRecorder();
        recorder2.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder2.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder2.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
        recorder2.setAudioEncodingBitRate(16);
        recorder2.setAudioSamplingRate(44100);
        recorder2.setOutputFile(output);
        recorder2.prepare();
        recorder2.start(); 
    }
    private static void ditchMediaRecorder2() {
        //if we have the recorder, release it
        if(recorder2 != null)
            recorder2.release(); }  
    static void stopRecording2() {
        if(recorder2 != null)
            recorder2.stop(); }

    static void beginRecording3(String output) throws Exception {
        ditchMediaRecorder3();
        File outFile = new File(output);        
        if(outFile.exists())
            outFile.delete();       
        recorder3 = new MediaRecorder();
        recorder3.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder3.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder3.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
        recorder3.setAudioEncodingBitRate(16);
        recorder3.setAudioSamplingRate(44100);
        recorder3.setOutputFile(output);
        recorder3.prepare();
        recorder3.start(); 
    }
    private static void ditchMediaRecorder3() {
        //if we have the recorder, release it
        if(recorder3 != null)
            recorder3.release(); }  
    static void stopRecording3() {
        if(recorder3 != null)
            recorder3.stop(); }

    static void beginRecording4(String output) throws Exception {
        ditchMediaRecorder4();
        File outFile = new File(output);        
        if(outFile.exists())
            outFile.delete();       
        recorder4 = new MediaRecorder();
        recorder4.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder4.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder4.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
        recorder4.setAudioEncodingBitRate(16);
        recorder4.setAudioSamplingRate(44100);
        recorder4.setOutputFile(output);
        recorder4.prepare();
        recorder4.start(); 
    }
    private static void ditchMediaRecorder4() {
        //if we have the recorder, release it
        if(recorder4 != null)
            recorder4.release(); }  
    static void stopRecording4() {
        if(recorder4 != null)
            recorder4.stop(); }

我一次录制两个文件时遇到的错误类型

04-14 20:34:01.676: E/MediaRecorder(21510): stop called in an invalid state: 0
04-14 20:34:01.676: E/InputEventReceiver(21510): Exception dispatching input event.
04-14 20:34:01.676: E/MessageQueue-JNI(21510): Exception in MessageQueue callback: handleReceiveCallback
04-14 20:34:01.686: E/MessageQueue-JNI(21510): java.lang.IllegalStateException
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.media.MediaRecorder.stop(Native Method)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at com.appulse.audiomate.RecordingHandler.stopRecording2(RecordingHandler.java:62)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at com.appulse.audiomate.SoundRecord$2.onTouch(SoundRecord.java:101)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.View.dispatchTouchEvent(View.java:7701)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2238)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.View.dispatchPointerEvent(View.java:7886)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.os.MessageQueue.nativePollOnce(Native Method)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.os.MessageQueue.next(MessageQueue.java:138)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.os.Looper.loop(Looper.java:123)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at android.app.ActivityThread.main(ActivityThread.java:5017)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at java.lang.reflect.Method.invokeNative(Native Method)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at java.lang.reflect.Method.invoke(Method.java:515)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-14 20:34:01.686: E/MessageQueue-JNI(21510):  at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案