gpt4 book ai didi

android 在不保存音频文件的情况下将麦克风记录到 ByteArray

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:57 26 4
gpt4 key购买 nike

抱歉我的英语不好:(

我开始我的 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
}
}

最佳答案

查看此答案!它完全适用于 MediaRecorder 而不是 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();

要将 byteArrayOutputStream 转换为 byte[],请使用 byteArrayOutputStream.toByteArray()

关于android 在不保存音频文件的情况下将麦克风记录到 ByteArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13974234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com