android将麦克风录制到ByteArray而不保存音频文件 [英] android record mic to ByteArray without saving audio file
问题描述
对不起,我英语不好:(
Sorry for my bad english :(
我启动了我的Android应用程序项目,即该应用程序的录音麦克风,如果单击开始录音按钮,则该应用程序将获取麦克风并将其写在文件上,单击停止则将文件保存到SD卡中.
I start my project for android application, this app record microphone, if I click to start record button the app get microphone and write it on a file and when I click to stop , a file saved into SD card.
项目代码:
输出文件
The output file
OUTPUT_FILE = Environment.getExternalStorageState() + "/myaudio.3gp";
开始录制
Start recording
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();
}
停止录制
Stop recording
public void stopRec(){
recorder.stop();
}
PlaySound录音文件
PlaySound recorded file
public void playRecFile() throws IOException{
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(OUTPUT_FILE);
mediaPlayer.prepare();
mediaPlayer.start();
}
我想获取录制的声音并将其放入变量ByteArray中,然后播放,将音频文件保存到SD卡中
I want to get recorded voice and put it into a variable ByteArray and play it whitout saving the audio file to SD card
我有一个想要的项目,但它是用动作脚本3编写的
I have a project like what I want but it is written in 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
将音频录制到字节数组:
Check Out this answer! It works exactly with MediaRecorder
not AudioRecord
Try to use the following solution to record audio to byte array using 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
中以开始执行.
I wrap this code in AsyncTask
for start execution.
此外,请不要忘记运行以下代码来停止录制:
Also, don't forgot to run the following code to stop recording:
recorder.stop();
recorder.reset();
recorder.release();
要将 byteArrayOutputStream
转换为 byte []
,请使用 byteArrayOutputStream.toByteArray()
这篇关于android将麦克风录制到ByteArray而不保存音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!