Skip to content

Commit

Permalink
Codec: Use AudioFormat.sampleRate directly
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Gunnerson <chillermillerlong@hotmail.com>
  • Loading branch information
chenxiaolong committed May 26, 2022
1 parent f83b8ee commit c9b35f4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/chiller3/bcr/RecorderThread.kt
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ class RecorderThread(
audioRecord.startRecording()

try {
val mediaFormat = codec.getMediaFormat(audioFormat, audioRecord.sampleRate, codecParam)
// audioRecord.format has the detected native sample rate
val mediaFormat = codec.getMediaFormat(audioRecord.format, codecParam)
val mediaCodec = codec.getMediaCodec(mediaFormat)

try {
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/chiller3/bcr/codec/Codec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,22 @@ sealed class Codec {
* Create a [MediaFormat] representing the encoded audio with parameters matching the specified
* input PCM audio format.
*
* @param audioFormat [AudioFormat.getSampleRate] must not be
* [AudioFormat.SAMPLE_RATE_UNSPECIFIED].
* @param param Codec-specific parameter value. Must be in the [paramRange] range. If null,
* [paramDefault] is used.
*
* @throws IllegalArgumentException if [param] is outside [paramRange]
*/
fun getMediaFormat(audioFormat: AudioFormat, sampleRate: Int, param: UInt?): MediaFormat {
fun getMediaFormat(audioFormat: AudioFormat, param: UInt?): MediaFormat {
if (param != null && param !in paramRange) {
throw IllegalArgumentException("Parameter $param not in range $paramRange")
}

val format = MediaFormat().apply {
setString(MediaFormat.KEY_MIME, mimeTypeAudio)
setInteger(MediaFormat.KEY_CHANNEL_COUNT, audioFormat.channelCount)
setInteger(MediaFormat.KEY_SAMPLE_RATE, sampleRate)
setInteger(MediaFormat.KEY_SAMPLE_RATE, audioFormat.sampleRate)
}

updateMediaFormat(format, param ?: paramDefault)
Expand Down

0 comments on commit c9b35f4

Please sign in to comment.