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

4

次のクラスを使用して、録音されたマイク データをバイト配列として取得します。データをバッファとして取得します..それを使用してみてください..これが役立つことを願っています..

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 に答える