下面列出了android.media.AudioFormat#CHANNEL_CONFIGURATION_MONO 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private int getMinBufferSize(int sampleRate, int channelConfig, int audioFormat) {
minBufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat);
// 解决异常IllegalArgumentException: Invalid audio buffer size
int channelCount = 1;
switch (channelConfig) {
// AudioFormat.CHANNEL_CONFIGURATION_DEFAULT
case AudioFormat.CHANNEL_OUT_DEFAULT:
case AudioFormat.CHANNEL_OUT_MONO:
case AudioFormat.CHANNEL_CONFIGURATION_MONO:
channelCount = 1;
break;
case AudioFormat.CHANNEL_OUT_STEREO:
case AudioFormat.CHANNEL_CONFIGURATION_STEREO:
channelCount = 2;
break;
default:
channelCount = Integer.bitCount(channelConfig);
}
// 判断minBufferSize是否在范围内,如果不在设定默认值为1152
int frameSizeInBytes = channelCount * (audioFormat == AudioFormat.ENCODING_PCM_8BIT ? 1 : 2);
if ((minBufferSize % frameSizeInBytes != 0) || (minBufferSize < 1)) {
minBufferSize = 1152;
}
return minBufferSize;
}
public AudioThread(int sampleRateInHz, int channel, long streamId, long decoderId, Media media)
{
if (channel == 1)
{
channel_configuration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
} else
{
channel_configuration = AudioFormat.CHANNEL_CONFIGURATION_STEREO;
}
this.mediaStreamId = streamId;
this.decoderId = decoderId;
this.media = media;
int minBufferSize = AudioTrack.getMinBufferSize(sampleRateInHz, channel_configuration, AudioFormat.ENCODING_PCM_16BIT);
if (minBufferSize > audioLength)
{
audioLength = minBufferSize;
}
mAudioBuffer = new byte[audioLength];
mAudio = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRateInHz, channel_configuration, AudioFormat.ENCODING_PCM_16BIT, audioLength, AudioTrack.MODE_STREAM);
}
private void startRecord(){
File file = new File(Environment.getExternalStorageDirectory(), "record.pcm");
int sampleFreq = (Integer)spFrequency.getSelectedItem();
try{
file.createNewFile();
OutputStream outputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);
int minBufferedSize = AudioRecord.getMinBufferSize(sampleFreq, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
short[] audioData = new short[minBufferedSize];
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleFreq, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferedSize);
audioRecord.startRecording();
while (recording) {
int numberOfShort = audioRecord.read(audioData, 0, minBufferedSize);
for(int i = 0; i < numberOfShort; i++){
dataOutputStream.writeShort(audioData[i]);
}
}
audioRecord.stop();
dataOutputStream.close();
} catch(Exception ex){
ex.printStackTrace();
}
}
/**
* Constructor.
*
* @param sampleRate
* The sample rate (e.g. 44100 Hz).
*/
public AudioRecorder(int sampleRate) {
int bufferSize = AudioRecord.getMinBufferSize(sampleRate,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize);
swtWindowing = false;
}