2011-07-16 152 views
0

我編寫了一個簡單的Android代碼,用於從我的HTC HD Desire Android Mobile中捕獲音頻,並將其粘貼到SD卡上的音頻文件中。我的代碼寫了音頻文件,但我無法打開文件。我懷疑音頻文件格式不正確。如何從我的Android手機麥克風中捕獲音頻

這是我編寫的代碼,並解釋了它的工作原理。我很感激關於如何調試我的代碼的任何提示。謝謝。

代碼

記錄器類具有一個方法,的StartRecording(),即負責從麥克風捕獲聲音20秒。

package com.audio; 

import android.media.AudioFormat; 
import android.media.AudioRecord; 
import android.media.MediaRecorder.AudioSource; 

public class Recorder { 

    private short[] recordedAudioBuffer; 
    private int bufferRead; 

    public short[] startRecording() { 

     int recorderBufferSize = AudioRecord.getMinBufferSize(8000, 
       AudioFormat.CHANNEL_CONFIGURATION_MONO, 
       AudioFormat.ENCODING_PCM_16BIT) * 2; 

     AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, 8000, 
       AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, 
       recorderBufferSize); 

     recordedAudioBuffer = new short[recorderBufferSize]; 
     recorder.startRecording(); 

     bufferRead = recorder.read(recordedAudioBuffer, 0, recorderBufferSize); 

     synchronized (this) { 
      try { 
       this.wait(20000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 

     recorder.stop(); 
     recorder.release(); 
     return recordedAudioBuffer; 

    } 

    public int getBufferRead() { 
     return bufferRead; 
    } 
} 

我用AudioRecorderActivity驅動錄音機代碼。此類使用包裝在BufferedOUtputStream中的簡單FileOutputStream,並將記錄器中的音頻緩衝區數組寫入文件。

package com.audio; 

import java.io.BufferedOutputStream; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Environment; 

public class AudioRecorderActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // create a file to dump the recording 
     File f = new File(Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + "/test.raw"); 
     try { 
      f.createNewFile(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // Record audio 
     Recorder recorderInstance = new Recorder(); 
     short[] recordedAudioBuffer = recorderInstance.startRecording(); 

     // Write the audio to the file 
     BufferedOutputStream bufferedStreamInstance = null; 

     try { 
      bufferedStreamInstance = new BufferedOutputStream(
        new FileOutputStream(f)); 
     } catch (FileNotFoundException e) { 
      throw new IllegalStateException("Cannot Open File", e); 
     } 

     DataOutputStream dataOutputStreamInstance = new DataOutputStream(
       bufferedStreamInstance); 

     try { 
      for (int idxBuffer = 0; idxBuffer < recordedAudioBuffer.length; idxBuffer++) { 
       dataOutputStreamInstance 
         .writeShort(recordedAudioBuffer[idxBuffer]); 
      } 
     } catch (IOException e) { 
      throw new IllegalStateException(
        "dataOutputStreamInstance.writeShort(curVal)"); 
     } 

    } 
} 

,這是我的Android清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.audio" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="9" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".AudioRecorderActivity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 

    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
</manifest> 

回答

0

通過AudioRecord產生的數據包括PCM採樣。這是聲音的「原始」形式。在上面的例子中,您將獲得每秒8,000個16位(兩個字節)的採樣。將這些數據寫入文件只是用這個原始數據創建一個文件。

你可能想要的是數據處於某種標準文件格式,它有一些標頭,可能還有一些壓縮(又名編碼)。

以已知格式錄製音頻的最簡單途徑是使用MediaRecorder類。 MediaRecorder的文檔有一個簡短的示例,我不會在這裏重複。

0

您可以使用一個意圖太多,如果你不想實現這個對你的應用程序

final Intent audioIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); 
      File audioFile = new File(Environment.getExternalStorageDirectory(), "videoTest.mp4"); 
      audioFileUri = Uri.fromFile(audioFile); 
      audioIntent.putExtra(MediaStore.EXTRA_OUTPUT, audioFileUri); 
      startActivityForResult(audioIntent, TAKE_AUDIO); 

audioFileUri是文件在SD卡中的URI

相關問題