android录制麦克风到ByteArray而不保存音频文件

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

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

解决方法

我想获取录制的声音,然后将音频文件保存到SD卡

使用AudioRecord类将麦克风中的音频抓取到阵列中,然后将其输入AudioTrack.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...