仿真器为什么会扭曲音频记录

时间:2019-08-01 12:32:23

标签: android android-emulator audio-recording

我正在将MacOS与Android模拟器一起使用来测试我的程序。在电话上测试相同程序时,录音效果会很好,但是在模拟器上进行测试时,录音声音会失真并且很奇怪。有人遇到过这样的事情吗?

fun prepare() {
        file?.delete()
        val audioSource = AudioSource.MIC
        val sampleRateInHz = 44100
        val channelConfig = AudioFormat.CHANNEL_IN_MONO
        val audioFormat = AudioFormat.ENCODING_PCM_16BIT
        val bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat)
        audioRecord = AudioRecord(audioSource, sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes);
        file = File(context.filesDir, "raw_voice_recording_${System.currentTimeMillis()}.pcm")
        if (!file!!.exists()) {
            file!!.createNewFile()
        }
    }
//...
    fun startRecording() {
        if (!isRecordingInProgress.get()) {
            isRecordingInProgress.set(true)
            audioRecord.startRecording()
        }
    }
//...
    fun record(): VoiceStreamAndAmplitude = synchronized(lock) {
        return if (isRecordingInProgress.get()) {
            val buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
            var outStream: FileOutputStream? = null
            try {
                outStream = FileOutputStream(file, true)
                val result = audioRecord.read(buffer, BUFFER_SIZE)
                if (result < 0) {
                    Squeak.SqueakBuilder.create(getBufferReadFailureReason(result), LogType.Error).send()
                    VoiceStreamAndAmplitude(ByteArray(0), 0f)
                } else {
                    outStream.write(buffer.array(), 0, BUFFER_SIZE);
                    buffer.clear();
                    VoiceStreamAndAmplitude(buffer.array(), 0f)
                }
            } finally {
                outStream?.close()
                buffer.clear()
            }
            VoiceStreamAndAmplitude(ByteArray(0), 0f)
        } else {
            VoiceStreamAndAmplitude(ByteArray(0), 0f)
        }
    }

0 个答案:

没有答案