Android TV box development how to use HDMI cable to transmit audio

Android TV box development how to use HDMI cable to transmit audio


I have a problem now, I developed a player on Android TV, when I simulate using the APP on the mobile phone, the mobile phone speaker can play the sound normally, but when I use the APP on the Android TV box, it is not Can’t get audio to my TV speakers via HDMI. At the same time, when I use the native app on the TV box to play video, the sound can be streamed to the TV’s speakers via HDMI for playback.

In my code, the following permissions are declared:

<uses-permission android:name="android.permission.MODIFY_AUDIO_ROUTING"
            tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

I have a method called initAudioManager defined as follows:

private void initAudioManager() {
        CustomSetting audioModeSetting = customSettingMapper.selectCustomSettingByKey(CustomSettingByKey.AUDIO_OUTPUT_MODE.getKey());
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        if (audioManager == null) {
            Log.e(TAG, "initAudioManager: audioManager is null");
            return;
        }
        if (audioModeSetting == null) {
            audioManager.setParameters("audio_devices_out_active=AUDIO_CODEC");
            Log.i(TAG, "initAudioManager: audioModeSetting is null,set audio_devices_out_active=AUDIO_CODEC");
        }
        if (audioModeSetting != null) {
            if (audioModeSetting.getSettingValue() == 0L) {
                audioManager.setParameters("audio_devices_out_active=AUDIO_CODEC");
                Log.i(TAG, "initAudioManager: audioModeSetting.getSettingValue() == 0L,set audio_devices_out_active=AUDIO_CODEC");
            } else {
                audioManager.setParameters("audio_devices_out_active=AUDIO_HDMI");
                Log.i(TAG, "initAudioManager: audioModeSetting.getSettingValue() == 1L,set audio_devices_out_active=AUDIO_HDMI");
            }
        }

        Log.i(TAG, "initAudioManager: " + audioManager.getParameters("audio_devices_out_active"));
        Log.i(TAG, "initAudioManager: " + Arrays.toString(audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)));
        MediaRouter mediaRouter = (MediaRouter) getSystemService(Context.MEDIA_ROUTER_SERVICE);
        if (mediaRouter == null) {
            Log.e(TAG, "initAudioManager: mediaRouter is null");
            return;
        }
        MediaRouter.RouteInfo selectedRoute = mediaRouter.getSelectedRoute(ROUTE_TYPE_LIVE_AUDIO);
        Log.d(TAG, "initAudioManager: " + selectedRoute.getName());
    }

When the audioModeSetting.getSettingValue() == 0L condition is not met, I will set the device audio output mode to HDMI, but it has no effect.

There is a place to print the log in my code, which is written like this:
Log.i(TAG, “initAudioManager: ” + audioManager.getParameters(“audio_devices_out_active”));
But when my program runs, the log printed here is empty and nothing.

What should I do to get my audio data to pass through the HDMI cable to the speakers of the TV?

See also  Web Design & Development Dubai, Custom Software Development UAE

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *