0

I found the code for storing audio in .wav format from this link.The code has two buttons to start and stop the recording.I want to stop the audio recording after sometime.It uses the AudioRecord class of Android to store audio.Is it possible to do so?

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
4

2 回答 2

0

在 MainActivity 的 onCreate 中使用此代码

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";
于 2013-04-18T08:32:03.963 回答
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();

感谢大家的帮助。

于 2013-04-26T09:13:45.413 回答