抱歉我的英语不好:(
我开始我的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
}
}
答案 0 :(得分:3)
我希望录制语音并将其放入变量ByteArray并将其播放,然后将音频文件保存到SD卡
使用AudioRecord类将麦克风中的音频抓取到阵列中,然后将其输入AudioTrack。
答案 1 :(得分:3)
使用以下类将录制的麦克风数据作为字节数组。您将获取数据作为缓冲区..尝试使用..希望这将帮助您..
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());
}
}
}
答案 2 :(得分:3)
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()