13

对不起,我的英语不好 :(

我为android应用程序启动我的项目,这个应用程序录制麦克风,如果我单击开始录制按钮,应用程序获取麦克风并将其写入文件,当我单击停止时,文件保存到SD卡中。

项目代码:

输出文件

OUTPUT_FILE = Environment.getExternalStorageState() + "/myaudio.3gp";

开始录制

public void startRecord() throws IOException{
    if (recorder != null)
    {
        recorder.release();
    }
    File outFile = new File(OUTPUT_FILE);
    if (outFile.exists())
    {
        outFile.delete();
    }

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setOutputFormat(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(OUTPUT_FILE);
    recorder.prepare();
    recorder.start();
}

停止录制

public void stopRec(){
    recorder.stop();
}

PlaySound 录制文件

public void playRecFile() throws IOException{
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(OUTPUT_FILE);
    mediaPlayer.prepare();
    mediaPlayer.start();

}

我想获取录制的语音并将其放入变量 ByteArray 并在不将音频文件保存到 SD 卡的情况下播放

我有一个我想要的项目,但它是用 actionscript 3 编写的

import flash.media.*;
import flash.events.*;
import flash.utils.ByteArray;

var ch:SoundChannel
var mic:Microphone = Microphone.getMicrophone();

mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
mic.addEventListener(ActivityEvent.ACTIVITY,onAct);

function onAct(evt:ActivityEvent):void
{
    trace(evt.activating,mic.activityLevel);
    if (!evt.activating)
    {
        if (soundBytes.length)
        {
            timerHandler();
        }
    }
}

var soundBytes:ByteArray = new ByteArray();
var soundO:ByteArray = new ByteArray();

function micSampleDataHandler(event:SampleDataEvent):void
{
    trace(event.data.length,event.data.bytesAvailable, soundBytes.length);
    while (event.data.bytesAvailable)
    {
        var sample:Number = event.data.readFloat();
        soundBytes.writeFloat(sample);
    }
}

function timerHandler():void
{
    mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
    soundBytes.position = 0;
    soundO.writeBytes(soundBytes);
    soundO.position = 0;
    soundBytes.position = 0;
    soundBytes.length=0;

    var sound:Sound= new Sound();
    sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
    ch=sound.play();
    ch.addEventListener(Event.SOUND_COMPLETE,onSC)
    trace("OUTPUT",soundO.bytesAvailable);

}
function onSC(evt:Event):void
{
    trace("SOUND_COMPLETE");
}
function playbackSampleHandler(event:SampleDataEvent):void
{
    trace("SAMPLE_DATA: ",soundO.bytesAvailable)
    for (var i:int = 0; i < 8192; i++)
    {
        if (soundO.bytesAvailable < 4)
        {
         break;
        }
        var sample:Number = soundO.readFloat();
        event.data.writeFloat(sample);
        event.data.writeFloat(sample);      
    }
    if (soundO.bytesAvailable < 4 && soundO.position!==0)
    {
        mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
        soundO.position=0
        soundO.length = 0;

        trace("END
    }
}
4

3 回答 3

9

看看这个答案!它与MediaRecordernot完全一致AudioRecord

尝试使用以下解决方案将音频录制到字节数组中MediaRecorder

    // Byte array for audio record
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    ParcelFileDescriptor[] descriptors = ParcelFileDescriptor.createPipe();
    ParcelFileDescriptor parcelRead = new ParcelFileDescriptor(descriptors[0]);
    ParcelFileDescriptor parcelWrite = new ParcelFileDescriptor(descriptors[1]);

    InputStream inputStream = new ParcelFileDescriptor.AutoCloseInputStream(parcelRead);

    MediaRecorder recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(parcelWrite.getFileDescriptor());
    recorder.prepare();

    recorder.start();


    int read;
    byte[] data = new byte[16384];

    while ((read = inputStream.read(data, 0, data.length)) != -1) {
        byteArrayOutputStream.write(data, 0, read);
    }

    byteArrayOutputStream.flush();

我将此代码包装起来AsyncTask以开始执行。

另外,不要忘记运行以下代码来停止录制:

    recorder.stop();
    recorder.reset();
    recorder.release();

转换byteArrayOutputStreambyte[]使用byteArrayOutputStream.toByteArray()

于 2017-03-12T17:13:31.453 回答
4

使用以下类将 Recorded Mic 数据作为字节数组获取。您将获得数据作为缓冲区..尝试使用它..希望这会对您有所帮助..

class AudioRecordThread implements Runnable {
        @Override
        public void run() {
            int bufferLength = 0;
            int bufferSize;
            short[] audioData;
            int bufferReadResult;

            try {
                bufferSize = AudioRecord.getMinBufferSize(sampleAudioBitRate, 
                        AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);

                if (bufferSize <= 2048) {
                    bufferLength = 2048;
                } else if (bufferSize <= 4096) {
                    bufferLength = 4096;
                }

                /* set audio recorder parameters, and start recording */
                audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleAudioBitRate, 
                        AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferLength);
                audioData = new short[bufferLength];
                audioRecord.startRecording();
                Log.d(LOG_TAG, "audioRecord.startRecording()");

                isAudioRecording = true;

                /* ffmpeg_audio encoding loop */
                while (isAudioRecording) {
                    bufferReadResult = audioRecord.read(audioData, 0, audioData.length);

                    if (bufferReadResult == 1024 && isRecorderStart) {
                        Buffer realAudioData1024 = ShortBuffer.wrap(audioData,0,1024);

                    *********************************** 
                        recorder.record(realAudioData1024);
                        ***********************************

                    } else if (bufferReadResult == 2048 && isRecorderStart) {
                        Buffer realAudioData2048_1=ShortBuffer.wrap(audioData, 0, 1024);
                        Buffer realAudioData2048_2=ShortBuffer.wrap(audioData, 1024, 1024);
                        for (int i = 0; i < 2; i++) {
                            if (i == 0) {
        ***********************************                     
        recorder.record(realAudioData2048_1);
        ***********************************                      

                            } else if (i == 1) {
***********************************
recorder.record(realAudioData2048_2);
***********************************


                            }
                        }
                    }
                }

                /* encoding finish, release recorder */
                if (audioRecord != null) {
                    try {
                        audioRecord.stop();
                        audioRecord.release();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    audioRecord = null;
                }

                if (recorder != null && isRecorderStart) {
                    try {
                        recorder.stop();
                        recorder.release();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    recorder = null;
                }
            } catch (Exception e) {
                Log.e(LOG_TAG, "get audio data failed:"+e.getMessage()+e.getCause()+e.toString());
            }

        }
    }
于 2012-12-20T14:33:27.340 回答
3

我想获取录制的语音并将其放入变量 ByteArray 并在不将音频文件保存到 SD 卡的情况下播放

使用AudioRecord类将麦克风中的音频抓取到一个数组中,然后将其输入到AudioTrack中。

于 2012-12-20T14:28:14.127 回答