|
| 1 | +/* |
| 2 | + * Copyright 2026 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.tv.ui |
| 18 | + |
| 19 | +import android.media.AudioAttributes |
| 20 | +import android.media.AudioDeviceInfo |
| 21 | +import android.media.AudioFormat |
| 22 | +import android.media.AudioManager |
| 23 | +import android.media.AudioTrack |
| 24 | +import android.os.Build |
| 25 | +import android.os.Handler |
| 26 | +import android.os.Looper |
| 27 | +import androidx.activity.ComponentActivity |
| 28 | +import androidx.annotation.RequiresApi |
| 29 | + |
| 30 | +class AudioCapabilitiesActivity : ComponentActivity() { |
| 31 | + private lateinit var audioManager: AudioManager |
| 32 | + |
| 33 | + @RequiresApi(Build.VERSION_CODES.TIRAMISU) |
| 34 | + fun anticipatoryAudioRouteCheck() { |
| 35 | + // [START android_tv_audio_capabilities_check] |
| 36 | + val format = AudioFormat.Builder() |
| 37 | + .setEncoding(AudioFormat.ENCODING_E_AC3) |
| 38 | + .setChannelMask(AudioFormat.CHANNEL_OUT_5POINT1) |
| 39 | + .setSampleRate(48000) |
| 40 | + .build() |
| 41 | + val attributes = AudioAttributes.Builder() |
| 42 | + .setUsage(AudioAttributes.USAGE_MEDIA) |
| 43 | + .build() |
| 44 | + |
| 45 | + if (AudioManager.getDirectPlaybackSupport(format, attributes) != |
| 46 | + AudioManager.DIRECT_PLAYBACK_NOT_SUPPORTED |
| 47 | + ) { |
| 48 | + // The format and attributes are supported for direct playback |
| 49 | + // on the currently active routed audio path |
| 50 | + } else { |
| 51 | + // The format and attributes are NOT supported for direct playback |
| 52 | + // on the currently active routed audio path |
| 53 | + } |
| 54 | + // [END android_tv_audio_capabilities_check] |
| 55 | + } |
| 56 | + |
| 57 | + private fun findBestSampleRate(profile: Any): Int = 48000 |
| 58 | + private fun findBestChannelMask(profile: Any): Int = AudioFormat.CHANNEL_OUT_STEREO |
| 59 | + |
| 60 | + @RequiresApi(Build.VERSION_CODES.TIRAMISU) |
| 61 | + // [START android_tv_audio_capabilities_best_format] |
| 62 | + private fun findBestAudioFormat(audioAttributes: AudioAttributes): AudioFormat { |
| 63 | + val preferredFormats = listOf( |
| 64 | + AudioFormat.ENCODING_E_AC3, |
| 65 | + AudioFormat.ENCODING_AC3, |
| 66 | + AudioFormat.ENCODING_PCM_16BIT, |
| 67 | + AudioFormat.ENCODING_DEFAULT |
| 68 | + ) |
| 69 | + val audioProfiles = audioManager.getDirectProfilesForAttributes(audioAttributes) |
| 70 | + val bestAudioProfile = preferredFormats.firstNotNullOf { format -> |
| 71 | + audioProfiles.firstOrNull { it.format == format } |
| 72 | + } |
| 73 | + val sampleRate = findBestSampleRate(bestAudioProfile) |
| 74 | + val channelMask = findBestChannelMask(bestAudioProfile) |
| 75 | + return AudioFormat.Builder() |
| 76 | + .setEncoding(bestAudioProfile.format) |
| 77 | + .setSampleRate(sampleRate) |
| 78 | + .setChannelMask(channelMask) |
| 79 | + .build() |
| 80 | + } |
| 81 | + // [END android_tv_audio_capabilities_best_format] |
| 82 | + |
| 83 | + private fun restartAudioTrack(info: AudioDeviceInfo?) {} |
| 84 | + private fun findDefaultAudioDeviceInfo(): AudioDeviceInfo? = null |
| 85 | + private fun needsAudioFormatChange(info: AudioDeviceInfo): Boolean = false |
| 86 | + |
| 87 | + fun interceptAudioDeviceChanges() { |
| 88 | + val audioPlayer = AudioPlayerWrapper() |
| 89 | + val handler = Handler(Looper.getMainLooper()) |
| 90 | + |
| 91 | + // [START android_tv_audio_capabilities_intercept] |
| 92 | + // audioPlayer is a wrapper around an AudioTrack |
| 93 | + // which calls a callback for an AudioTrack write error |
| 94 | + audioPlayer.addAudioTrackWriteErrorListener { |
| 95 | + // error code can be checked here, |
| 96 | + // in case of write error try to recreate the audio track |
| 97 | + restartAudioTrack(findDefaultAudioDeviceInfo()) |
| 98 | + } |
| 99 | + |
| 100 | + audioPlayer.audioTrack.addOnRoutingChangedListener({ audioRouting -> |
| 101 | + audioRouting?.routedDevice?.let { audioDeviceInfo -> |
| 102 | + // use the updated audio routed device to determine |
| 103 | + // what audio format should be used |
| 104 | + if (needsAudioFormatChange(audioDeviceInfo)) { |
| 105 | + restartAudioTrack(audioDeviceInfo) |
| 106 | + } |
| 107 | + } |
| 108 | + }, handler) |
| 109 | + // [END android_tv_audio_capabilities_intercept] |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +private class AudioPlayerWrapper { |
| 114 | + val audioTrack = AudioTrack.Builder().build() |
| 115 | + fun addAudioTrackWriteErrorListener(listener: () -> Unit) {} |
| 116 | +} |
0 commit comments