AudioRecorder.kt 26.2 KB
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
package uts.sdk.modules.uniRecorder

import android.media.AudioFormat
import android.media.AudioRecord
import android.media.MediaCodec
import android.media.MediaCodecInfo
import android.media.MediaFormat
import android.media.MediaMuxer
import android.media.MediaRecorder
import android.os.Handler
import android.os.Looper
import android.util.Log
// import io.dcloud.feature.audio.mp3.SimpleLame
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.RandomAccessFile
import java.util.Timer
import java.util.TimerTask

class AudioRecorder() {
    private var audioRecord: AudioRecord? = null
    var isRecording = false
    var isPaused = false
    private var filePath: String? = null
    private var format: RecordingFormat? = null
    private var recordingThread: Thread? = null
    private var sampleRate = 8000 // 默认采样率
    private var channelConfig = AudioFormat.CHANNEL_IN_STEREO // 默认双声道
    private var audioEncoding = AudioFormat.ENCODING_PCM_16BIT // 默认16位
    private var bitRate = 128000 // 默认码率 (AAC/MP3)
    private var recordingListener: RecordingListener? = null
    private var audioDataListener: AudioDataListener? = null
    private var saveToFile = true
    private var mediaCodec: MediaCodec? = null
    private var mediaMuxer: MediaMuxer? = null
    private var audioTrackIndex = -1
    private var lameInitialized = false
    private val mainHandler = Handler(Looper.getMainLooper())
    private var audioBuffer: ByteArray? = null

    // 新增变量用于处理录制时长
    private var maxRecordingDuration: Long = 600000 // 默认最大录制时长为 600000 毫秒
    private var recordingStartTime: Long = 0 // 录音开始时间
    private var pauseStartTime: Long = 0 // 暂停开始时间
    private var totalPauseDuration: Long = 0 // 累计暂停时长
    private var timer: Timer? = null // 定时器用于检查录制时长

    private var presentationTimeUs: Long = 0

    // 定义错误码常量
    companion object {
        const val ERROR_RECORDING_IN_PROGRESS = 1001
        const val ERROR_NO_SUPPORTED_SAMPLE_RATE = 1107602
        const val ERROR_BUFFER_SIZE_CALCULATION_FAILED = 1107603
        const val ERROR_AUDIO_RECORD_INIT_FAILED = 1107604
        const val ERROR_AAC_ENCODER_NOT_SUPPORTED = 1107605
        const val ERROR_INITIALIZATION_FAILED = 1107604
        const val ERROR_RECORDING_START_FAILED = 1107604
        const val ERROR_FILE_CREATION_FAILED = 1008
        const val ERROR_FILE_WRITE_FAILED = 1009
        const val ERROR_CANNOT_MODIFY_DURING_RECORDING = 1010
        const val ERROR_UNKNOWN = 9999
        const val ERROR_PERMISSION_DENIED = 1107601
        const val ERROR_INVALID_FILE_PATH = 1012
        const val ERROR_FILE_ALREADY_EXISTS = 1013
    }

    fun getFilePath(): String? {
        return filePath
    }

    // 设置录音参数
    fun setAudioParams(sampleRate: Int, channels: Int, bitRate: Int) {
        if (isRecording) {
            notifyError(ERROR_CANNOT_MODIFY_DURING_RECORDING, "录音正在进行,无法修改录音参数")
            return
        }

        // 校验并设置采样率,如果传入值非法,则使用默认值 44100
        this.sampleRate = if (sampleRate > 0) sampleRate else 44100

        // 校验并设置码率,如果传入值非法,则使用默认值 128000
        this.bitRate = if (bitRate > 0) bitRate else 128000

        // 校验并设置通道配置,非法值将默认设置为双通道 (STEREO)
        this.channelConfig = when (channels) {
            1 -> AudioFormat.CHANNEL_IN_MONO
            2 -> AudioFormat.CHANNEL_IN_STEREO
            else -> AudioFormat.CHANNEL_IN_STEREO // 默认使用双通道
        }
    }

    // 设置各种监听器
    fun setRecordingListener(listener: RecordingListener?) {
        this.recordingListener = listener
    }

    fun setAudioDataListener(listener: AudioDataListener?) {
        this.audioDataListener = listener
    }

    // 设置是否保存到文件
    fun setSaveToFile(saveToFile: Boolean) {
        if (isRecording) {
            notifyError(ERROR_CANNOT_MODIFY_DURING_RECORDING, "录音正在进行,无法修改保存设置")
            return
        }
        this.saveToFile = saveToFile
    }

    // 设置最大录制时长
    fun setMaxRecordingDuration(duration: Long) {
        if (isRecording) {
            notifyError(ERROR_CANNOT_MODIFY_DURING_RECORDING, "录音正在进行,无法修改最大录制时长")
            return
        }
        this.maxRecordingDuration = duration
    }

    fun startRecording(format: RecordingFormat, fileName: String) {
        if (isRecording) {
            notifyError(ERROR_RECORDING_IN_PROGRESS, "已有录音操作正在进行")
            return
        }

        this.format = format

        // 校验文件路径
        if (saveToFile) {
            if (fileName.isBlank()) {
                notifyError(ERROR_INVALID_FILE_PATH, "文件路径不能为空")
                return
            }
            val file = File(fileName)
            if (file.isDirectory) {
                notifyError(ERROR_INVALID_FILE_PATH, "提供的路径是一个目录,而不是文件")
                return
            }
            if (file.exists()) {
                notifyError(ERROR_FILE_ALREADY_EXISTS, "文件已存在: $fileName")
                return
            }
            val parentDir = file.parentFile
            if (parentDir != null && !parentDir.exists()) {
                if (!parentDir.mkdirs()) {
                    notifyError(ERROR_FILE_CREATION_FAILED, "无法创建父目录")
                    return
                }
            }
            this.filePath = fileName
        } else {
            this.filePath = null
        }
        Log.d("AudioRecorder", "File path set to: $filePath")


        // 检测并调整采样率
        sampleRate = checkAndAdjustSampleRate()
        if (sampleRate == -1) {
            notifyError(ERROR_NO_SUPPORTED_SAMPLE_RATE, "没有支持的采样率")
            return
        }

        val bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioEncoding)
        if (bufferSize == AudioRecord.ERROR_BAD_VALUE || bufferSize == AudioRecord.ERROR) {
            notifyError(ERROR_BUFFER_SIZE_CALCULATION_FAILED, "计算缓冲区大小失败")
            return
        }

        try {
            audioRecord = AudioRecord(
                MediaRecorder.AudioSource.MIC,
                sampleRate,
                channelConfig,
                audioEncoding,
                bufferSize
            )
        } catch (e: SecurityException) {
            notifyError(ERROR_PERMISSION_DENIED, "录音权限被拒绝: ${e.message}")
            return
        }


        if (audioRecord?.state != AudioRecord.STATE_INITIALIZED) {
            notifyError(ERROR_AUDIO_RECORD_INIT_FAILED, "AudioRecord 初始化失败")
            audioRecord?.release()
            audioRecord = null
            return
        }

        try {
            when (format) {
                RecordingFormat.AAC -> {
                    if (!checkAacEncoderSupport()) {
                        notifyError(ERROR_AAC_ENCODER_NOT_SUPPORTED, "不支持 AAC 编码")
                        return
                    }
                    initAacEncoder()
                }

                RecordingFormat.MP3 -> {
                    initLame()
                }

                RecordingFormat.WAV -> {
                    if (saveToFile && filePath != null) {
                        FileOutputStream(filePath).use { os ->
                            writeWavHeaderPlaceholder(os)
                        }
                    }
                }

                else -> {}
            }
        } catch (e: IOException) {
            notifyError(ERROR_INITIALIZATION_FAILED, "启动失败: ${e.message}")
            cleanupResources()
            return
        }

        audioRecord?.startRecording()
        if (audioRecord?.recordingState != AudioRecord.RECORDSTATE_RECORDING) {
            notifyError(ERROR_RECORDING_START_FAILED, "录音启动失败")
            cleanupResources()
            return
        }
        isRecording = true
        isPaused = false
        presentationTimeUs = 0 // 在这里重置

        // 记录录音开始时间
        recordingStartTime = System.currentTimeMillis()
        totalPauseDuration = 0

        // 启动定时器检查录制时长
        // timer = Timer()
        // timer?.scheduleAtFixedRate(object : TimerTask() {
        //     override fun run() {
        //         if (isRecording && !isPaused) {
        //             val actualRecordingTime =
        //                 System.currentTimeMillis() - recordingStartTime - totalPauseDuration
        //             if (actualRecordingTime >= maxRecordingDuration) {
        //                 mainHandler.post {
        //                     stopRecording()
        //                 }
        //                 cancel()
        //             }
        //         }
        //     }
        // }, 100, 100) // 每 100 毫秒检查一次

        audioBuffer = ByteArray(bufferSize)

        recordingThread = Thread({
            var os: FileOutputStream? = null

            try {
                if (saveToFile && filePath != null && format != RecordingFormat.AAC) {
                    os = FileOutputStream(filePath, true)
                }

                while (isRecording) {
                    val buffer = audioBuffer!!
                    val read = audioRecord?.read(buffer, 0, buffer.size) ?: -1
                    if (read != AudioRecord.ERROR_INVALID_OPERATION && read > 0) {
                        // 回调音频数据
                        if (audioDataListener != null && !isPaused) {
                            val dataCopy = buffer.copyOf(read)
                            mainHandler.post {
                                audioDataListener?.onAudioData(dataCopy, read)
                            }
                        }

                        // 处理保存逻辑
                        if (!isPaused && saveToFile && filePath != null) {
                            when (format) {
                                RecordingFormat.AAC -> encodeAac(buffer, read)
                                RecordingFormat.MP3 -> encodeToMp3(buffer, read, os)
                                RecordingFormat.PCM, RecordingFormat.WAV -> os?.write(
                                    buffer,
                                    0,
                                    read
                                )
                            }
                        }
                    }
                }
            } catch (e: IOException) {
                notifyError(ERROR_FILE_WRITE_FAILED, "录音过程中写入文件出错: ${e.message}")
            } finally {
                // 清理资源
                cleanupResources()

                // 完成文件处理
                os?.use {
                    try {
                        it.flush()
                        if (format == RecordingFormat.WAV && saveToFile && filePath != null) {
                            updateWavHeader(filePath!!, sampleRate, getChannelCount(), 16)
                        }
                    } catch (e: IOException) {
                        notifyError(ERROR_FILE_WRITE_FAILED, "文件处理出错: ${e.message}")
                    }
                }
            }
        }, "AudioRecorder Thread")

        recordingThread?.start()
        mainHandler.post {
            recordingListener?.onRecordingStarted()
        }
    }

    private fun initAacEncoder() {
        val audioFormat = MediaFormat.createAudioFormat(
            MediaFormat.MIMETYPE_AUDIO_AAC,
            sampleRate,
            getChannelCount()
        )
        audioFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitRate)
        audioFormat.setInteger(
            MediaFormat.KEY_AAC_PROFILE,
            MediaCodecInfo.CodecProfileLevel.AACObjectLC
        )

        mediaCodec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC)
        mediaCodec?.configure(audioFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
        mediaCodec?.start()

        mediaMuxer = MediaMuxer(filePath!!, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4)
    }

    private fun encodeAac(buffer: ByteArray, length: Int) {
        if (isPaused) return
        var offset = 0
        while (offset < length && isRecording) {
            val inputBufferIndex = mediaCodec?.dequeueInputBuffer(-1) ?: -1
            if (inputBufferIndex >= 0) {
                val inputBuffer = mediaCodec?.getInputBuffer(inputBufferIndex)
                inputBuffer?.clear()

                val capacity = inputBuffer?.capacity() ?: 0
                val size = if (length - offset > capacity) capacity else length - offset

                if (size > 0) {
                    inputBuffer?.put(buffer, offset, size)

                    // 使用手动维护的时间戳
                    mediaCodec?.queueInputBuffer(
                        inputBufferIndex,
                        0,
                        size,
                        presentationTimeUs,
                        0
                    )
                    // 根据写入的数据量计算下一帧的时间戳
                    // 时间戳单位为微秒 (us)
                    // (size * 1_000_000L) / (sampleRate * channels * bytesPerSample)
                    presentationTimeUs += (size.toLong() * 1000000) / (sampleRate * getChannelCount() * 2)

                    offset += size
                }
            }

            // 处理编码后的数据
            val bufferInfo = MediaCodec.BufferInfo()
            var outputBufferIndex = mediaCodec?.dequeueOutputBuffer(bufferInfo, 0) ?: -1
            while (outputBufferIndex >= 0) {
                val outputBuffer = mediaCodec?.getOutputBuffer(outputBufferIndex)
                if (audioTrackIndex == -1) {
                    audioTrackIndex = mediaMuxer?.addTrack(mediaCodec?.outputFormat!!) ?: -1
                    mediaMuxer?.start()
                }
                outputBuffer?.position(bufferInfo.offset)
                outputBuffer?.limit(bufferInfo.offset + bufferInfo.size)
                if (bufferInfo.size > 0 && bufferInfo.presentationTimeUs > 0) {
                    mediaMuxer?.writeSampleData(audioTrackIndex, outputBuffer!!, bufferInfo)
                }
                mediaCodec?.releaseOutputBuffer(outputBufferIndex, false)
                outputBufferIndex = mediaCodec?.dequeueOutputBuffer(bufferInfo, 0) ?: -1
            }
        }
    }

    private fun initLame() {
        val channels = getChannelCount()
        SimpleLameBridge.init(sampleRate, channels, sampleRate, bitRate / 1000, 7)
        lameInitialized = true
    }

    private fun encodeToMp3(pcmBuffer: ByteArray, pcmSize: Int, outputStream: FileOutputStream?) {
        if (!lameInitialized) return

        val channels = getChannelCount()
        // 调整MP3缓冲区大小以适应最坏情况
        val mp3Buffer = ByteArray((pcmSize * 1.25).toInt() + 7200)
        val encodedSize: Int

        if (channels == 1) {
            // 单声道处理
            val sampleCount = pcmSize / 2
            val shortBuffer = ShortArray(sampleCount)
            for (i in 0 until sampleCount) {
                shortBuffer[i] = ((pcmBuffer[2 * i].toInt() and 0xFF) or
                        (pcmBuffer[2 * i + 1].toInt() shl 8)).toShort()
            }
            // 假设SimpleLame.encode对于单声道,第二个声道参数传null
            encodedSize = SimpleLameBridge.encode(shortBuffer, shortBuffer, sampleCount, mp3Buffer)
        } else {
            // 立体声处理
            // 每个采样点占用4字节(左右声道各2字节),所以采样点数是 pcmSize / 4
            val sampleCount = pcmSize / 4
            val leftChannel = ShortArray(sampleCount)
            val rightChannel = ShortArray(sampleCount)

            for (i in 0 until sampleCount) {
                // 从交错的PCM数据中分离左右声道
                // 左声道样本
                leftChannel[i] = ((pcmBuffer[4 * i].toInt() and 0xFF) or
                        (pcmBuffer[4 * i + 1].toInt() shl 8)).toShort()
                // 右声道样本
                rightChannel[i] = ((pcmBuffer[4 * i + 2].toInt() and 0xFF) or
                        (pcmBuffer[4 * i + 3].toInt() shl 8)).toShort()
            }
            encodedSize = SimpleLameBridge.encode(leftChannel, rightChannel, sampleCount, mp3Buffer)
        }

        if (encodedSize > 0) {
            try {
                outputStream?.write(mp3Buffer, 0, encodedSize)
            } catch (e: IOException) {
                notifyError(ERROR_FILE_WRITE_FAILED, "写入MP3文件失败: ${e.message}")
            }
        }
    }

    fun pauseRecording() {
        if (!isRecording || isPaused) return
        isPaused = true
        // 记录暂停开始时间
        pauseStartTime = System.currentTimeMillis()
        mainHandler.post {
            recordingListener?.onRecordingPaused()
        }
    }

    fun resumeRecording() {
        if (!isRecording || !isPaused) return
        isPaused = false
        // 计算本次暂停时长并累加到总暂停时长
        totalPauseDuration += System.currentTimeMillis() - pauseStartTime
        mainHandler.post {
            recordingListener?.onRecordingResumed()
        }
    }

    fun stopRecording() {
        if (!isRecording) return
        isRecording = false
        isPaused = false

        // 取消定时器
        timer?.cancel()
        timer = null

        // 等待录音线程结束,避免竞态条件
        try {
            recordingThread?.join()
        } catch (e: InterruptedException) {
            Thread.currentThread().interrupt()
            Log.e("AudioRecorder", "Recording thread interrupted", e)
        }
        recordingThread = null

        // 线程结束后再清理资源
        when (format) {
            RecordingFormat.AAC -> {
                try {
                    mediaCodec?.signalEndOfInputStream()
                    val bufferInfo = MediaCodec.BufferInfo()
                    var outputBufferIndex = mediaCodec?.dequeueOutputBuffer(bufferInfo, 10000) ?: -1
                    while (outputBufferIndex >= 0) {
                        val outputBuffer = mediaCodec?.getOutputBuffer(outputBufferIndex)
                        if (audioTrackIndex != -1 && outputBuffer != null && bufferInfo.size > 0 && bufferInfo.presentationTimeUs > 0) {
                            outputBuffer.position(bufferInfo.offset)
                            outputBuffer.limit(bufferInfo.offset + bufferInfo.size)
                            mediaMuxer?.writeSampleData(audioTrackIndex, outputBuffer, bufferInfo)
                        }
                        mediaCodec?.releaseOutputBuffer(outputBufferIndex, false)
                        outputBufferIndex = mediaCodec?.dequeueOutputBuffer(bufferInfo, 10000) ?: -1
                    }
                } catch (e: Exception) {
                    Log.e("AudioRecorder", "Error during AAC stream end signaling", e)
                } finally {
                    try {
                        mediaCodec?.stop()
                        mediaCodec?.release()
                    } catch (e: Exception) {
                        Log.e("AudioRecorder", "MediaCodec stop/release failed", e)
                    }
                    try {
                        mediaMuxer?.stop()
                        mediaMuxer?.release()
                    } catch (e: Exception) {
                        Log.e("AudioRecorder", "MediaMuxer stop/release failed", e)
                    }
                    mediaCodec = null
                    mediaMuxer = null
                    audioTrackIndex = -1
                }
            }

            RecordingFormat.MP3 -> {
                if (lameInitialized) {
                    try {
                        val mp3Buffer = ByteArray(7200)
                        val encodedSize = SimpleLameBridge.flush(mp3Buffer)
                        if (encodedSize > 0) {
                            FileOutputStream(filePath, true).use {
                                it.write(
                                    mp3Buffer,
                                    0,
                                    encodedSize
                                )
                            }
                        }
                    } catch (e: IOException) {
                        notifyError(ERROR_FILE_WRITE_FAILED, "MP3 flush failed: ${e.message}")
                    } finally {
                        SimpleLameBridge.close()
                        lameInitialized = false
                    }
                }
            }

            else -> {}
        }

        // audioRecord 的清理可以保留在这里
        try {
            audioRecord?.stop()
            audioRecord?.release()
        } catch (e: Exception) {
            Log.e("AudioRecorder", "AudioRecord stop/release failed", e)
        }
        audioRecord = null

        mainHandler.post {
            recordingListener?.onRecordingStopped()
        }
    }

    private fun cleanupResources() {
        try {
            audioRecord?.stop()
            audioRecord?.release()
        } catch (e: Exception) {
            Log.e("AudioRecorder", "AudioRecord cleanup failed: ${e.message}")
        }
        audioRecord = null

        if (lameInitialized) {
            SimpleLameBridge.close()
            lameInitialized = false
        }
        audioBuffer = null
    }

    private fun getChannelCount(): Int {
        return if (channelConfig == AudioFormat.CHANNEL_IN_MONO) 1 else 2
    }

    // WAV文件头处理方法
    private fun writeWavHeaderPlaceholder(os: FileOutputStream) {
        // RIFF header
        os.write("RIFF".toByteArray())
        os.write(intToByteArray(0)) // 文件大小,稍后更新
        os.write("WAVE".toByteArray())

        // fmt chunk
        os.write("fmt ".toByteArray())
        os.write(intToByteArray(16)) // fmt chunk大小
        os.write(shortToByteArray(1.toShort())) // 音频格式:PCM
        os.write(shortToByteArray(getChannelCount().toShort())) // 通道数
        os.write(intToByteArray(sampleRate)) // 采样率
        os.write(intToByteArray(sampleRate * getChannelCount() * 2)) // 字节率
        os.write(shortToByteArray((getChannelCount() * 2).toShort())) // 块对齐
        os.write(shortToByteArray(16.toShort())) // 位深度

        // data chunk
        os.write("data".toByteArray())
        os.write(intToByteArray(0)) // 数据大小,稍后更新
    }

    private fun updateWavHeader(filePath: String, sampleRate: Int, channels: Int, bitDepth: Int) {
        try {
            val file = File(filePath)
            val fileSize = file.length()
            val dataSize = fileSize - 44 // WAV文件头大小为44字节

            val raf = RandomAccessFile(file, "rw")

            // 更新RIFF chunk大小
            raf.seek(4)
            raf.write(intToByteArray((fileSize - 8).toInt()))

            // 更新采样率相关参数(如果需要)
            raf.seek(24)
            raf.write(intToByteArray(sampleRate))

            raf.seek(28)
            raf.write(intToByteArray(sampleRate * channels * (bitDepth / 8)))

            // 更新data chunk大小
            raf.seek(40)
            raf.write(intToByteArray(dataSize.toInt()))

            raf.close()
        } catch (e: IOException) {
            notifyError(ERROR_FILE_WRITE_FAILED, "更新 WAV 文件头出错: ${e.message}")
        }
    }

    // 辅助方法:将int转换为字节数组(小端序)
    private fun intToByteArray(value: Int): ByteArray {
        return byteArrayOf(
            (value and 0xFF).toByte(),
            ((value shr 8) and 0xFF).toByte(),
            ((value shr 16) and 0xFF).toByte(),
            ((value shr 24) and 0xFF).toByte()
        )
    }

    // 辅助方法:将short转换为字节数组(小端序)
    private fun shortToByteArray(value: Short): ByteArray {
        return byteArrayOf(
            (value.toInt() and 0xFF).toByte(),
            ((value.toInt() shr 8) and 0xFF).toByte()
        )
    }

    private fun checkAndAdjustSampleRate(): Int {
        // 优先检查当前设置的采样率是否支持
        if (sampleRate > 0) {
            val bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioEncoding)
            if (bufferSize > 0) {
                return sampleRate
            }
        }

        val supportedSampleRates = intArrayOf(44100, 48000, 22050, 16000, 11025, 8000)
        for (rate in supportedSampleRates) {
            if (rate == sampleRate) continue
            val bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioEncoding)
            if (bufferSize > 0) {
                return rate
            }
        }
        return -1
    }

    private fun checkAacEncoderSupport(): Boolean {
        val audioFormat = MediaFormat.createAudioFormat(
            MediaFormat.MIMETYPE_AUDIO_AAC,
            sampleRate,
            getChannelCount()
        )
        audioFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitRate)
        audioFormat.setInteger(
            MediaFormat.KEY_AAC_PROFILE,
            MediaCodecInfo.CodecProfileLevel.AACObjectLC
        )

        try {
            val codec = MediaCodec.createEncoderByType(MediaFormat.MIMETYPE_AUDIO_AAC)
            codec.configure(audioFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
            codec.release()
            return true
        } catch (e: Exception) {
            return false
        }
    }

    private fun notifyError(errorCode: Int, errorMessage: String) {
        mainHandler.post {
            recordingListener?.onRecordingError(errorCode, errorMessage)
            audioDataListener?.onAudioError(errorCode, errorMessage)
        }
    }

    fun release() {
        stopRecording()
        recordingListener = null
        audioDataListener = null
    }
}

enum class RecordingFormat {
    AAC, PCM, MP3, WAV
}

// 录音状态回调接口
interface RecordingListener {
    fun onRecordingStarted()
    fun onRecordingStopped()
    fun onRecordingPaused()
    fun onRecordingResumed()
    fun onRecordingError(errorCode: Int, errorMessage: String)
}

// 音频数据回调接口
interface AudioDataListener {
    fun onAudioData(data: ByteArray, size: Int)
    fun onAudioError(errorCode: Int, errorMessage: String)
}