isRecording.set(false)
}
// 释放录音资源
override fun release() {
audioRecord.release()
}
}
下面是 MediaRecorder 对于Recorder
接口的实现:
inner class MediaRecord(override var outputFormat: String) : Recorder {
private var starTime = AtomicLong() // 音频录制开始时间
// 监听录制是否超时的回调
private val listener = MediaRecorder.OnInfoListener { _, what, _ ->
when (what) {
MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED -> {
// 如果录制超时,则停止录制会回调上层
stop()
handleRecordEnd(isSuccess = true, isReachMaxTime = true)
}
else -> {
handleRecordEnd(isSuccess = false, isReachMaxTime = false)
}
}
}
// 录制错误监听器
private val errorListener = MediaRecorder.OnErrorListener { _, _, _ ->
handleRecordEnd(isSuccess = false, isReachMaxTime = false)
}
private val recorder = MediaRecorder()
private var isRecording = AtomicBoolean(false) // 用于控制音频录制的线程安全布尔值
private var duration = 0L // 音频时长
// 判断是否正在录制音频
override fun isRecording(): Boolean = isRecording.get()
// 录制音频时长
override fun getDuration(): Long = duration
// 开始录制音频
override fun start(outputFile: File, maxDuration: Int) {
// 枚举音频输出格式
val format = when (outputFormat) {
AMR -> MediaRecorder.OutputFormat.AMR_NB
else -> MediaRecorder.OutputFormat.AAC_ADTS
}
// 枚举音频编码格式
val encoder = when (outputFormat) {
AMR -> MediaRecorder.AudioEncoder.AMR_NB
else -> MediaRecorder.AudioEncoder.AAC
}
// 开始录制
starTime.set(SystemClock.elapsedRealtime())
isRecording.set(true)
recorder.apply {
reset()
setAudioSource(MediaRecorder.AudioSource.MIC)
setOutputFormat(format)
setOutputFile(outputFile.absolutePath)
setAudioEncoder(encoder)
setOnInfoListener(listener)
setOnErrorListener(errorListener)
setMaxDuration(maxDuration)
prepare()
start()
}
}
// 停止录制
override fun stop() {
recorder.stop()
isRecording.set(false)
duration = SystemClock.elapsedRealtime() - starTime.get()
}
// 释放录制资源
override fun release() {
recorder.release()
}
}
把和Recorder
接口打交道的上层类定义为AudioManager
,它是业务层访问音频能力的入口,提供了一组访问接口:
// 构造 AudioManager 时需传入上下文和音频输出格式
class AudioManager(val context: Context, val type: String = AAC) {
companion object {
const val AAC = “aac”
const val AMR = “amr”
const val PCM = “pcm”
}
private var maxDuration = 120 * 1000 // 默认最大音频时长为 120 s
// 根据输出格式实例化对应 Recorder 实例
private var recorder: Recorder = if (type == PCM) AudioRecorder(type) else MediaRecord(type)
// 开始录制
fun start(maxDuration: Int = 120) {
this.maxDuration = maxDuration * 1000
startRecord()
}
// 停止录制
fun stop(cancel: Boolean = false) {
stopRecord(cancel)
}
// 释放资源
fun release() {
recorder.release()
}
// 是否正在录制
fun isRecording() = recorder.isRecording()
}
其中的startRecord()
和stopRecord()
包含了AudioManager
层控制播放的逻辑:
class AudioManager(val context: Context, val type: String = AAC) :
// 为了方便启动协程录制,直接继承 CoroutineScope,并调度协程到一个单线程线程池对应的 Dispatcher
CoroutineScope by CoroutineScope(SupervisorJob() + Executors.newFixedThreadPool(1).asCoroutineDispatcher()) {
private var recorder: Recorder = if (type == PCM) AudioRecorder(type) else MediaRecord(type)
// 开始录制
private fun startRecord() {
// 请求音频焦点
audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
// 若音频正在录制,则返回
if (recorder.isRecording()) {
setState(STATE_FAILED) // 设置状态为失败
return
}
// 如果储存卡控件不足,则返回
if (getFreeSpace() <= 0) {