在AudioRecord类中自动停止录制

时间:2013-04-18 08:25:04

标签: android audio

我从这个link找到了以.wav格式存储音频的代码。代码有两个按钮来启动和停止录音。我想在一段时间后停止录音。它使用AudioRecord类Android存储音频。是否可以这样做?

04-19 08:20:00.688: E/AndroidRuntime(2405): FATAL EXCEPTION: main
04-19 08:20:00.688: E/AndroidRuntime(2405): java.lang.RuntimeException: Unable to start    activity   ComponentInfo{com.example.androidwaverecorder/com.example.androidwaverecorder.MainActivity}:  java.lang.NullPointerException
04-19 08:20:00.688: E/AndroidRuntime(2405):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at android.os.Looper.loop(Looper.java:137)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at android.app.ActivityThread.main(ActivityThread.java:5039)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at java.lang.reflect.Method.invoke(Method.java:511)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at dalvik.system.NativeStart.main(Native Method)
04-19 08:20:00.688: E/AndroidRuntime(2405): Caused by: java.lang.NullPointerException
04-19 08:20:00.688: E/AndroidRuntime(2405):     at   com.example.androidwaverecorder.MainActivity.onCreate(MainActivity.java:47)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at  android.app.Activity.performCreate(Activity.java:5104)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-19 08:20:00.688: E/AndroidRuntime(2405):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-19 08:20:00.688: E/AndroidRuntime(2405):     ... 11 more
04-19 08:31:55.766: E/Trace(2899): error opening trace file: No such file or directory (2)
04-19 08:31:55.766: W/Trace(2899): Unexpected value from nativeGetEnabledTags: 0
04-19 08:31:55.766: W/Trace(2899): Unexpected value from nativeGetEnabledTags: 0
04-19 08:31:55.766: W/Trace(2899): Unexpected value from nativeGetEnabledTags: 0
04-19 08:31:56.386: W/Trace(2899): Unexpected value from nativeGetEnabledTags: 0
04-19 08:31:56.386: W/Trace(2899): Unexpected value from nativeGetEnabledTags: 0
04-19 08:31:56.416: W/Trace(2899): Unexpected value from nativeGetEnabledTags: 0
04-19 08:31:56.416: W/Trace(2899): Unexpected value from nativeGetEnabledTags: 0

2 个答案:

答案 0 :(得分:0)

在onCreate of MainActivity中使用此代码

final Runnable r = new Runnable()
    {
        public void run() 
        {
            if (WavAudioRecorder.State.ERROR == mRecorder.getState()) {
                        mRecorder.release();
                        mRecorder = WavAudioRecorder.getInstanse();
                        mRecorder.setOutputFile(mRcordFilePath);
                        btnControl.setText("Start");
                    } else {
                        mRecorder.stop();
                        mRecorder.reset();
                        btnControl.setText("Start");
                    }
                }
        }
    };

    handler.postDelayed(r, YOUR TIME IN MILLISEC);

修改

 private static final String mRcordFilePath = Environment.getExternalStorageDirectory() + "/testwave.wav";

String mRcordFilePath = Environment.getExternalStorageDirectory() + "/testwave.wav";

答案 1 :(得分:0)

我找到了解决方案 您可以使用AsyncTask来执行此操作。

如何做的简要说明: 当你开始录音时,在另一个班级中创建一个线程,该线程将在所需的时间段内休眠,然后调用你的录音机停止功能。

public class Auto_Stop_Task extends AsyncTask<Void,Void, Integer> {
//int flag=0;
@Override
protected Integer doInBackground(Void... arg0) {

    try {
        Thread.sleep(30000);
        MainActivity.mRecorder.stop();
        MainActivity.mRecorder.reset();
        //flag=1;
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    return null;
}

}

启动录音机后调用此课程。

new Auto_Stop_Task().execute();

感谢大家的帮助。