android – AudioRecord – 编写PCM文件

前端之家收集整理的这篇文章主要介绍了android – AudioRecord – 编写PCM文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用AudioRecord类录制一些语音,然后将其写入输出.pcm文件.我希望我的程序继续录制,直到按下停止按钮.不幸的是,无论我录制多长时间,输出文件大小总是3528字节,它持续约20毫秒.另外根据Toolsoft Audio Tools,该文件的特性是:44100Hz,16位,立体声,即使我使用具有完全不同采样频率的单声道.
Thread recordingThread;
boolean isRecording = false;


int audioSource = AudioSource.MIC;
int sampleRateInHz = 44100;
int channelConfig = AudioFormat.CHANNEL_IN_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,channelConfig,audioFormat);

byte Data[] = new byte[bufferSizeInBytes];

AudioRecord audioRecorder = new AudioRecord(audioSource,sampleRateInHz,audioFormat,bufferSizeInBytes);
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);   
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main,menu);
    return true;
}    

public void startRecording(View arg0) {
    audioRecorder.startRecording();
    isRecording = true;
    recordingThread = new Thread(new Runnable() {
        public void run() {
            String filepath = Environment.getExternalStorageDirectory().getPath();
            FileOutputStream os = null;
            try {
                os = new FileOutputStream(filepath+"/record.pcm");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            while(isRecording) {
                audioRecorder.read(Data,Data.length);
                try {
                    os.write(Data,bufferSizeInBytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    recordingThread.start();
}

public void stopRecording(View arg0) {
    if (null != audioRecorder) {
        isRecording = false;
        audioRecorder.stop();
        audioRecorder.release();
        audioRecorder = null;
        recordingThread = null;
    }
}

我可以请你告诉我有什么问题吗?我希望答案不会是’一切’:)

解决方法

将您的采样率更改为8000,因为在模拟器中您无法使用44100采样率进行测试.

如图所示使用AudioRecord源在模拟器中播放

private static final int RECORDER_SAMPLERATE = 8000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUdio_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

AudioRecord audio_record = new AudioRecord(MediaRecorder.AudioSource.MIC,RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUdio_ENCODING,BufferElements2Rec * BytesPerElement);


int BufferElements2Play = 1024; // want to play 2048 (2K) since 2 bytes we use only 1024
int BytesPerElement = 2; // 2 bytes in 16bit format

If it din’t get worked,then go to this blog. It will work fine for recording and storing the data in .pcm file.

猜你在找的Android相关文章