需要一个简单的录音示例

发布于 2024-11-10 01:56:59 字数 78 浏览 4 评论 0原文

我需要在 android 中使用 AudioRecorder 进行简单的音频录制和播放示例。我尝试使用 MediaRecorder,效果很好。

I am in need of simple audio recording and playing example using AudioRecorder in android. I tried with MediaRecorder, it works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘逸的'云 2024-11-17 01:56:59

你是说AudioRecord?使用 Google 代码搜索搜索例如“AudioRecord.OnRecordPositionUpdateListener”。顺便说一句,AudioRecord 进行录音,而不是播放。

另请参阅:

You mean AudioRecord? Search e.g. "AudioRecord.OnRecordPositionUpdateListener" using Google Code Search. Btw, AudioRecord does recording, not playing.

See also:

ぃ双果 2024-11-17 01:56:59

这是音频记录的示例代码。

    private Runnable recordRunnable = new Runnable() {

    @Override
    public void run() {

        byte[] audiodata = new byte[mBufferSizeInBytes];
        int readsize = 0;

        Log.d(TAG, "start to record");
        // start the audio recording
        try {
            mAudioRecord.startRecording();
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
        }

        // in the loop to read data from audio and save it to file.
        while (mInRecording == true) {
            readsize = mAudioRecord.read(audiodata, 0, mBufferSizeInBytes);
            if (AudioRecord.ERROR_INVALID_OPERATION != readsize
                    && mFos != null) {
                try {
                    mFos.write(audiodata);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // stop recording
        try {
            mAudioRecord.stop();
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
        }

        getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                mRecordLogTextView.append("\n Audio finishes recording");
            }
        });

        // close the file
        try {
            if (mFos != null)
                mFos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
};

那么你需要两个按钮(或者一个按钮在不同的时间充当不同的功能)来启动和停止记录线程。

        mRecordStartButton = (Button) rootView
            .findViewById(R.id.audio_record_start);

    mRecordStartButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // initialize the audio source
            int recordChannel = getChoosedSampleChannelForRecord();
            int recordFrequency = getChoosedSampleFrequencyForRecord();
            int recordBits = getChoosedSampleBitsForRecord();

            Log.d(TAG, "recordBits = " + recordBits);

            mRecordChannel = getChoosedSampleChannelForSave();
            mRecordBits = getChoosedSampleBitsForSave();
            mRecordFrequency = recordFrequency;

            // set up the audio source : get the buffer size for audio
            // record.
            int minBufferSizeInBytes = AudioRecord.getMinBufferSize(
                    recordFrequency, recordChannel, recordBits);

            if(AudioRecord.ERROR_BAD_VALUE == minBufferSizeInBytes){

                mRecordLogTextView.setText("Configuration Error");
                return;
            }

            int bufferSizeInBytes = minBufferSizeInBytes * 4;

            // create AudioRecord object
            mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    recordFrequency, recordChannel, recordBits,
                    bufferSizeInBytes);

            // calculate the buffer size used in the file operation.
            mBufferSizeInBytes = minBufferSizeInBytes * 2;

            // reset the save file setup
            String rawFilePath = WaveFileWrapper
                    .getRawFilePath(RAW_PCM_FILE_NAME);

            try {
                File file = new File(rawFilePath);
                if (file.exists()) {
                    file.delete();
                }

                mFos = new FileOutputStream(file);

            } catch (Exception e) {
                e.printStackTrace();
            }

            if (mInRecording == false) {

                mRecordThread = new Thread(recordRunnable);
                mRecordThread.setName("Demo.AudioRecord");
                mRecordThread.start();

                mRecordLogTextView.setText(" Audio starts recording");

                mInRecording = true;

                // enable the stop button
                mRecordStopButton.setEnabled(true);

                // disable the start button
                mRecordStartButton.setEnabled(false);
            }

            // show the log info
            String audioInfo = " Audio Information : \n"
                    + " sample rate = " + mRecordFrequency + "\n"
                    + " channel = " + mRecordChannel + "\n"
                    + " sample byte = " + mRecordBits;
            mRecordLogTextView.setText(audioInfo);

        }
    });

    mRecordStopButton = (Button) rootView
            .findViewById(R.id.audio_record_stop);
    mRecordStopButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mInRecording == false) {

                Log.d(TAG, "current NOT in Record");

            } else {

                // stop recording
                if (mRecordThread != null) {

                    Log.d(TAG, "mRecordThread is not null");

                    mInRecording = false;

                    Log.d(TAG, "set mInRecording to false");

                    try {
                        mRecordThread.join(TIMEOUT_FOR_RECORD_THREAD_JOIN);
                        Log.d(TAG, "record thread joins here");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    mRecordThread = null;

                    // re-enable the start button
                    mRecordStartButton.setEnabled(true);

                    // disable the start button
                    mRecordStopButton.setEnabled(false);

                } else {
                    Log.d(TAG, "mRecordThread is null");
                }
            }
        }
    });

然后您可以将 pcm 数据保存到 WAV 文件中。

here is the sample code for audio record.

    private Runnable recordRunnable = new Runnable() {

    @Override
    public void run() {

        byte[] audiodata = new byte[mBufferSizeInBytes];
        int readsize = 0;

        Log.d(TAG, "start to record");
        // start the audio recording
        try {
            mAudioRecord.startRecording();
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
        }

        // in the loop to read data from audio and save it to file.
        while (mInRecording == true) {
            readsize = mAudioRecord.read(audiodata, 0, mBufferSizeInBytes);
            if (AudioRecord.ERROR_INVALID_OPERATION != readsize
                    && mFos != null) {
                try {
                    mFos.write(audiodata);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // stop recording
        try {
            mAudioRecord.stop();
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
        }

        getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                mRecordLogTextView.append("\n Audio finishes recording");
            }
        });

        // close the file
        try {
            if (mFos != null)
                mFos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
};

then you need two buttons (or one acts as different function in the different time) to start and stop the record thread.

        mRecordStartButton = (Button) rootView
            .findViewById(R.id.audio_record_start);

    mRecordStartButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // initialize the audio source
            int recordChannel = getChoosedSampleChannelForRecord();
            int recordFrequency = getChoosedSampleFrequencyForRecord();
            int recordBits = getChoosedSampleBitsForRecord();

            Log.d(TAG, "recordBits = " + recordBits);

            mRecordChannel = getChoosedSampleChannelForSave();
            mRecordBits = getChoosedSampleBitsForSave();
            mRecordFrequency = recordFrequency;

            // set up the audio source : get the buffer size for audio
            // record.
            int minBufferSizeInBytes = AudioRecord.getMinBufferSize(
                    recordFrequency, recordChannel, recordBits);

            if(AudioRecord.ERROR_BAD_VALUE == minBufferSizeInBytes){

                mRecordLogTextView.setText("Configuration Error");
                return;
            }

            int bufferSizeInBytes = minBufferSizeInBytes * 4;

            // create AudioRecord object
            mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    recordFrequency, recordChannel, recordBits,
                    bufferSizeInBytes);

            // calculate the buffer size used in the file operation.
            mBufferSizeInBytes = minBufferSizeInBytes * 2;

            // reset the save file setup
            String rawFilePath = WaveFileWrapper
                    .getRawFilePath(RAW_PCM_FILE_NAME);

            try {
                File file = new File(rawFilePath);
                if (file.exists()) {
                    file.delete();
                }

                mFos = new FileOutputStream(file);

            } catch (Exception e) {
                e.printStackTrace();
            }

            if (mInRecording == false) {

                mRecordThread = new Thread(recordRunnable);
                mRecordThread.setName("Demo.AudioRecord");
                mRecordThread.start();

                mRecordLogTextView.setText(" Audio starts recording");

                mInRecording = true;

                // enable the stop button
                mRecordStopButton.setEnabled(true);

                // disable the start button
                mRecordStartButton.setEnabled(false);
            }

            // show the log info
            String audioInfo = " Audio Information : \n"
                    + " sample rate = " + mRecordFrequency + "\n"
                    + " channel = " + mRecordChannel + "\n"
                    + " sample byte = " + mRecordBits;
            mRecordLogTextView.setText(audioInfo);

        }
    });

    mRecordStopButton = (Button) rootView
            .findViewById(R.id.audio_record_stop);
    mRecordStopButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mInRecording == false) {

                Log.d(TAG, "current NOT in Record");

            } else {

                // stop recording
                if (mRecordThread != null) {

                    Log.d(TAG, "mRecordThread is not null");

                    mInRecording = false;

                    Log.d(TAG, "set mInRecording to false");

                    try {
                        mRecordThread.join(TIMEOUT_FOR_RECORD_THREAD_JOIN);
                        Log.d(TAG, "record thread joins here");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    mRecordThread = null;

                    // re-enable the start button
                    mRecordStartButton.setEnabled(true);

                    // disable the start button
                    mRecordStopButton.setEnabled(false);

                } else {
                    Log.d(TAG, "mRecordThread is null");
                }
            }
        }
    });

then you can save the pcm data into a WAV file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文