2012-12-20 75 views
4

對不起我的英語不好:(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和發揮它whitout的音頻文件保存到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

我認爲你有一個錯誤:更換'Environment.getExternalStorageState() '環境'。get OUternalStorageDirectory()'在你的OUTPUT_FILE名字中。 –

回答

1

我想要得到的錄音,並把它變成一個變量的ByteArray和發揮它whitout的音頻文件保存到SD卡

使用日e AudioRecord類可以將麥克風的音頻抓取到數組中,然後將其饋送到AudioTrack

+0

這就是我需要的,你有任何代碼使用? –

+0

請參閱Rajesh關於如何設置和使用AudioRecord的答案。 AudioTrack的過程非常相似。我相信你可以找到例子,如果你在這裏看看周圍的計算器。 – Michael

+0

非常感謝Michael先生 –

2

使用以下類獲取錄製的麥克風數據作爲字節數組。你將得到的數據作爲緩衝區..嘗試使用..希望這將幫助您..

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()); 
      } 

     } 
    } 
+0

謝謝,但是劇本代碼在哪裏? –

+0

我必須去搜索它.. – itsrajesh4uguys

+0

這很好,我會嘗試更多的時間,謝謝 –

2

看看這個答案!它的工作原理正好與MediaRecorderAudioRecord

嘗試使用以下解決方案來錄製音頻使用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(); 

要轉換byteArrayOutputStreambyte[]使用byteArrayOutputStream.toByteArray()

相關問題