Android:从不同设备录制和播放音频

huangapple go评论74阅读模式
英文:

Android: Record and Playback audio from different devices

问题

我正在尝试使用不同的设备使用AudioRecordAudioTrack录制和播放音频,并设置preferredDevice来设置每个设备。

audioRecord = AudioRecord(
                        MediaRecorder.AudioSource.VOICE_COMMUNICATION,
                        sampleRate,
                        AudioFormat.CHANNEL_IN_MONO,
                        AudioFormat.ENCODING_PCM_FLOAT,
                        frameSize * Float.SIZE_BYTES
                    )
audioRecord?.preferredDevice = mic

audioTrack = AudioTrack.Builder()
                    .setAudioAttributes(
                        AudioAttributes.Builder()
                            .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
                            .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                            .build()
                    )
                    .setAudioFormat(
                        AudioFormat.Builder()
                            .setEncoding(AudioFormat.ENCODING_PCM_FLOAT)
                            .setSampleRate(PLAYBACK_SAMPLE_RATE)
                            .setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
                            .build()
                    )
                    .setBufferSizeInBytes(frameSize * Float.SIZE_BYTES * 2)
                    .build()

audioTrack?.preferredDevice = speaker

当输入和输出都首选一个设备时,它能正常工作,但当选择不同的设备时,麦克风(AudioRecord)也会使用扬声器的首选设备。即使我记录audioRecord?.routedDevice,它似乎不会更改为AudioRecord的首选设备,而是使用相同的首选AudioTrack

注意:我正在尝试在有线耳机、蓝牙设备和Android设备的默认音频硬件之间切换。

有什么可能是问题的原因吗?或者在Android中是否不可能像在iOS中一样同时使用两个设备进行输入和输出?

英文:

I'm trying to record and play audio using different devices with AudioRecord and AudioTrack and setting preferredDevice to set the devices to each.

    audioRecord = AudioRecord(
                            MediaRecorder.AudioSource.VOICE_COMMUNICATION,
                            sampleRate,
                            AudioFormat.CHANNEL_IN_MONO,
                            AudioFormat.ENCODING_PCM_FLOAT,
                            frameSize * Float.SIZE_BYTES
                        )
    audioRecord?.preferredDevice = mic

    audioTrack = AudioTrack.Builder()
                    .setAudioAttributes(
                        AudioAttributes.Builder()
                            .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
                            .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                            .build()
                    )
                    .setAudioFormat(
                        AudioFormat.Builder()
                            .setEncoding(AudioFormat.ENCODING_PCM_FLOAT)
                            .setSampleRate(PLAYBACK_SAMPLE_RATE)
                            .setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
                            .build()
                    )
                    .setBufferSizeInBytes(frameSize * Float.SIZE_BYTES * 2)
                    .build()

    audioTrack?.preferredDevice = speaker

It works fine when both input and out are preferred to one device, but when selecting different devices, the mic(AudioRecord) is also using the speaker's preferred device. Even when I log the audioRecord?.routedDevice, it doesn't seem to change to the preferred device for AudioRecord but uses the same preferred AudioTrack.

Note: I'm trying to switch between a Wired headset, a Bluetooth device and the Android device's default audio hardware.

Any idea what could be the issue here? Or is it not possible to use two devices for input and output like in iOS?

答案1

得分: 1

你需要检查setPreferredDevice是否返回true。

如果指定的AudioDeviceInfo非空且不对应有效的音频输入/输出设备,则返回false。

我假设micspeaker不为空,因为如果deviceInfo为空,将恢复默认路由。

英文:

You need to check if setPreferredDevice returns true.

It returns false if the specified AudioDeviceInfo is non-null and does not correspond to a valid audio input/output device.

I assumed mic and speaker is not null because if deviceInfo is null, default routing is restored.

huangapple
  • 本文由 发表于 2023年7月20日 10:25:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定