Android音视频(一) Camera2 API采集数据
AudioRecord和AudioTrack是Android系统提供的用于实现录音、播放音频的功能类,使用这两个类做音频的采集与播放还是非常简单的。
AudioRecord
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| private void startRecorder() { try { mAudioFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/RecorderTest/" + System.currentTimeMillis() + ".pcm"); mAudioFile.getParentFile().mkdirs(); mAudioFile.createNewFile(); mFileOutputStream = new FileOutputStream(mAudioFile);
int audioSource = MediaRecorder.AudioSource.MIC; int sampleRate = 44100; int channelConfig = AudioFormat.CHANNEL_IN_MONO; int audioFormat = AudioFormat.ENCODING_PCM_16BIT; int minBufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat); mAudioRecorder = new AudioRecord(audioSource, sampleRate, channelConfig, audioFormat, Math.max(minBufferSize, 2048)); mAudioRecorder.startRecording();
while (mIsRecording) { int read = mAudioRecorder.read(mBuffer, 0, 2048); if (read > 0) { mFileOutputStream.write(mBuffer, 0, read); } } mAudioRecorder.stop(); mAudioRecorder.release(); mAudioRecorder = null; mFileOutputStream.close();
} catch (IOException | RuntimeException e) { e.printStackTrace(); } finally { if (mAudioRecorder != null) { mAudioRecorder.release(); mAudioRecorder = null; } } }
|
Android SDK 中有两套音频采集的API,分别是:MediaRecorder 和 AudioRecord。
MediaRecorder是一个更加上层一点的API,它可以直接把手机麦克风录入的音频数据进行编码压缩(如AMR、MP3等)并存成文件
AudioRecord则更接近底层,能够更加自由灵活地控制,可以得到原始的一帧帧PCM音频数据。
如果只是想简单地做一个录音机,录制音频文件,就使用 MediaRecorder,而如果需要对音频做进一步的算法处理、或者采用第三方的编码库进行压缩、以及网络传输、直播等应用,则建议使用 AudioRecord。
AudioTrack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| private void doPaly(File mAudioFile) { int streamType = AudioManager.STREAM_MUSIC; int sampleRate = 44100; int channelConfig = AudioFormat.CHANNEL_OUT_MONO; int audioFormat = AudioFormat.ENCODING_PCM_16BIT; int mode = AudioTrack.MODE_STREAM;
int minBufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat);
AudioTrack audioTrack = new AudioTrack(streamType, sampleRate, channelConfig, audioFormat, Math.max(minBufferSize, 2048), mode);
FileInputStream mFileInputStream = null; try { mFileInputStream = new FileInputStream(mAudioFile); int read; audioTrack.play(); while ((read = mFileInputStream.read(mBuffer)) > 0) { int ret = audioTrack.write(mBuffer, 0, read); switch (ret) { case AudioTrack.ERROR_BAD_VALUE: case AudioTrack.ERROR_INVALID_OPERATION: case AudioManager.ERROR_DEAD_OBJECT: palyFaile(); break; default: break; } } } catch (RuntimeException | IOException e) { e.printStackTrace(); palyFaile(); } finally { mIsPalying = false; if (mFileInputStream != null) { closeQuietly(mFileInputStream); } audioTrack.stop(); audioTrack.release(); } }
|
在Android中播放声音也是有两套API:MediaPlayer和AudioTrack,两者还是有很大的区别的。
MediaPlayer可以播放多种格式的声音文件,例如MP3,AAC,WAV,OGG,MIDI等。MediaPlayer会在framework层创建对应的音频解码器。
AudioTrack只能播放已经解码的PCM流,如不需要解码的wav文件。
MediaPlayer在framework层还是会创建AudioTrack,把解码后的PCM数流传递给AudioTrack,AudioTrack再传递给AudioFlinger进行混音,然后才传递给硬件播放,所以是MediaPlayer包含了AudioTrack。
Demo