英文:
Android: Record and Playback audio from different devices
问题
我正在尝试使用不同的设备使用AudioRecord
和AudioTrack
录制和播放音频,并设置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。
我假设mic
和speaker
不为空,因为如果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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论