将音频记录保存到文件对象

时间:2013-08-27 21:27:26

标签: java android file audio-recording

我试图将音频记录从麦克风保存到文件对象而不是文件。为什么?因为我只需要保存记录,而App是活跃的。这是我尝试使用developer.android.com的“音频捕获”教程

private Button b1;
private Button b2;

private ProgressBar bar;

private MediaRecorder mRecorder = null;
private MediaPlayer mPlayer = null;

private boolean recording = false;
private boolean playing = false;

public AudioRecordDialog(Context context, final MainActivity main) {
    super(context);
    setTitle(R.string.setupSound);
    setContentView(R.layout.audio_record_dialog_layout);

    bar = (ProgressBar) findViewById(R.id.progressBar1);
    bar.setAlpha(0);

    b1 = (Button) findViewById(R.id.button1);
    b1.setText(R.string.startButton);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (recording) {
                b1.setText(R.string.startButton);
                recording = false;
                bar.setAlpha(0);
                if (mRecorder != null) {
                    mRecorder.stop();
                    mRecorder.release();
                }
                mRecorder = null;
            } else {
                b1.setText(R.string.stopButton);
                recording = true;
                bar.setAlpha(1);

                mRecorder = new MediaRecorder();
                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                mRecorder.setOutputFile("record.3gp");

                try {
                    mRecorder.prepare();
                } catch (IOException e) {
                    Toast toast = Toast.makeText(getContext(),
                            "Failed Recording!", Toast.LENGTH_SHORT);
                    toast.show();
                }

                mRecorder.start();
            }
        }
    });

    b2 = (Button) findViewById(R.id.button2);
    b2.setText(R.string.playButton);
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (playing) {
                b2.setText(R.string.playButton);
                playing = false;
                if (mPlayer != null)
                    mPlayer.release();
                mPlayer = null;
            } else {
                b2.setText(R.string.pauseButton);
                playing = true;
                mPlayer = new MediaPlayer();
                try {
                    mPlayer.setDataSource("record.3gp");
                    mPlayer.prepare();
                    mPlayer.start();
                } catch (IOException e) {
                    Toast toast = Toast.makeText(getContext(),
                                    "Failed Playing soundFile!",
                                    Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        }
    });

但单击记录按钮时它不起作用并崩溃。 这是logCat。

08-28 02:07:14.144: E/Trace(17588): error opening trace file: Permission denied (13)
08-28 02:07:14.324: D/SensorManager(17588): caller of registerListener:17588,sensor type:3
08-28 02:07:14.410: D/libEGL(17588): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
08-28 02:07:14.425: D/libEGL(17588): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
08-28 02:07:14.425: D/libEGL(17588): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
08-28 02:07:14.511: D/OpenGLRenderer(17588): Enabling debug mode 0
08-28 02:07:17.785: E/MediaRecorder(17588): start called in an invalid state: 4
08-28 02:07:17.785: D/AndroidRuntime(17588): Shutting down VM
08-28 02:07:17.785: W/dalvikvm(17588): threadid=1: thread exiting with uncaught exception (group=0x40ddd2a0)
08-28 02:07:17.793: E/AndroidRuntime(17588): FATAL EXCEPTION: main
08-28 02:07:17.793: E/AndroidRuntime(17588): java.lang.IllegalStateException
08-28 02:07:17.793: E/AndroidRuntime(17588):    at android.media.MediaRecorder.start(Native Method)
08-28 02:07:17.793: E/AndroidRuntime(17588):    at com.notfall.notruf.AudioRecordDialog$1.onClick(AudioRecordDialog.java:74)
08-28 02:07:17.793: E/AndroidRuntime(17588):    at android.view.View.performClick(View.java:4106)
08-28 02:07:17.793: E/AndroidRuntime(17588):    at android.view.View$PerformClick.run(View.java:17052)
08-28 02:07:17.793: E/AndroidRuntime(17588):    at android.os.Handler.handleCallback(Handler.java:615)
08-28 02:07:17.793: E/AndroidRuntime(17588):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-28 02:07:17.793: E/AndroidRuntime(17588):    at android.os.Looper.loop(Looper.java:137)
08-28 02:07:17.793: E/AndroidRuntime(17588):    at android.app.ActivityThread.main(ActivityThread.java:5118)
08-28 02:07:17.793: E/AndroidRuntime(17588):    at java.lang.reflect.Method.invokeNative(Native Method)
08-28 02:07:17.793: E/AndroidRuntime(17588):    at java.lang.reflect.Method.invoke(Method.java:511)
08-28 02:07:17.793: E/AndroidRuntime(17588):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
08-28 02:07:17.793: E/AndroidRuntime(17588):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
08-28 02:07:17.793: E/AndroidRuntime(17588):    at dalvik.system.NativeStart.main(Native Method)
08-28 02:07:19.191: I/Process(17588): Sending signal. PID: 17588 SIG: 9

它崩溃了

mRecorder.start();

1 个答案:

答案 0 :(得分:0)

您应该在setAudioEncoder()之后致电setOutputFormat()