问题
I have an app where one device shares his internal audio to another android device using the Sound Capture API (that was introduced in the Android API level 29). This app is working fine on mobile devices.
However, when I try the app on an android box (which has access to the Android level 29 API) which is connected to a TV via HDMI, it doesn't seem to receive the audio signal. So either the audio can't be transferred or there is an issue when setting the audio format channel mask.
Below is my code:
val config = AudioPlaybackCaptureConfiguration.Builder(AudioCaptureService.mediaProjection!!)
.addMatchingUsage(AudioAttributes.USAGE_MEDIA)
.addMatchingUsage(AudioAttributes.USAGE_UNKNOWN)
.addMatchingUsage(AudioAttributes.USAGE_GAME)
.build()
val buffer = Buffer()
val audioFormat = AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(buffer.sampleRate)
.setChannelMask(AudioFormat.CHANNEL_IN_MONO)
.build()
val record = AudioRecord.Builder()
.setAudioFormat(audioFormat)
.setAudioPlaybackCaptureConfig(config)
.setBufferSizeInBytes(buffer.size)
.build()
回答1:
The problem that I can see does not come from your code but rather from the API you are using. When reading the spec (see Playback capture on the Android developers website), there is this line:
This API gives apps the ability to copy the audio being played by other apps.
If I understand correctly the design of your app, you stream an audio feed from an android device (let's call it the sender) to another device (this one will be the receiver). The sound capture step occurs on the sender, the device that sends the stream to the receiver. Not on the receiver.
Now, if you wish to record the stream coming from a TV (no matter how you get this stream) with your app, you would need to install your app on the TV because it is the one that sends the signal. If you could do that, you would reproduce the scheme sender -> receiver and everything would (should) work fine.
However, as I understand it, you are only able to install the app on the receiver (the Android box), making the audio capture impossible with this design.
To solve this, you would need to record an input stream with the MediaRecorder API and the correct AudioSource
(see MediaRecorder.AudioSource). There is a guide on how to record audio right here: MediaRecorder overview.
来源:https://stackoverflow.com/questions/64196648/record-internal-audio-not-working-for-tv-box