Android TV पर एक साथ कई ऑडियो आउटपुट इस्तेमाल किए जा सकते हैं. जैसे, टीवी के स्पीकर, एचडीएमआई होम सिनेमा, ब्लूटूथ वगैरह. इन डिवाइसों पर अलग-अलग तरह की एन्कोडिंग (जैसे, Dolby Digital+, DTS, PCM), सैंपल रेट, और चैनल इस्तेमाल किए जा सकते हैं. एचडीएमआई से कनेक्ट किए गए टीवी पर कई फ़ॉर्मैट इस्तेमाल किए जा सकते हैं. हालांकि, ब्लूटूथ पर सिर्फ़ पीसीएम फ़ॉर्मैट इस्तेमाल किया जा सकता है.
प्लेबैक के दौरान, उपलब्ध ऑडियो डिवाइस और राउटिंग में बदलाव किया जा सकता है. जैसे, उपयोगकर्ता एचडीएमआई को प्लग इन कर सकते हैं, ब्लूटूथ कनेक्ट कर सकते हैं या सेटिंग में बदलाव कर सकते हैं. ऐप्लिकेशन को नए डिवाइस की सुविधाओं के हिसाब से काम करना चाहिए, ताकि प्लेबैक जारी रहे. जिन फ़ॉर्मैट के साथ डिवाइस काम नहीं करते हैं उनमें गड़बड़ियां हो सकती हैं या आवाज़ नहीं आ सकती है.
डिवाइस की सुविधाओं के हिसाब से, बेहतर अनुभव के लिए कई एन्कोडिंग उपलब्ध कराएं. उदाहरण के लिए, अगर Dolby Digital इस्तेमाल किया जा सकता है, तो उसका इस्तेमाल करें. ऐसा न होने पर, ऐप्लिकेशन पीसीएम पर वापस आ जाता है. Android डिकोडर के लिए, इस्तेमाल किए जा सकने वाले मीडिया फ़ॉर्मैट देखें. ये डिकोडर, स्ट्रीम को पीसीएम में बदलते हैं.
प्लेबैक के दौरान, आपके स्ट्रीमिंग ऐप्लिकेशन को आउटपुट ऑडियो डिवाइस के साथ काम करने वाले
सबसे बेहतर AudioFormat के साथ AudioTrack बनाना चाहिए.
सही फ़ॉर्मैट में ट्रैक बनाना
ऐप्लिकेशन को AudioTrack बनाना चाहिए, उसे चलाना चाहिए, और आवाज़ चलाने के लिए डिफ़ॉल्ट ऑडियो डिवाइस का पता लगाने के लिए,
getRoutedDevice() को कॉल करना चाहिए. उदाहरण के लिए, यह एक सुरक्षित, कम समय के लिए साइलेंस वाला पीसीएम एन्कोड किया गया ट्रैक हो सकता है. इसका इस्तेमाल सिर्फ़ राउट किए गए डिवाइस और उसकी ऑडियो सुविधाओं का पता लगाने के लिए किया जाता है.
इस्तेमाल की जा सकने वाली एन्कोडिंग पाना
डिफ़ॉल्ट ऑडियो डिवाइस पर उपलब्ध ऑडियो फ़ॉर्मैट का पता लगाने के लिए, getAudioProfiles() (एपीआई लेवल 31 और इसके बाद के लेवल) या getEncodings()
(एपीआई लेवल 23 और इसके बाद के लेवल) का इस्तेमाल करें.
इस्तेमाल किए जा सकने वाले ऑडियो प्रोफ़ाइल और फ़ॉर्मैट देखना
फ़ॉर्मैट, चैनल की संख्या, और सैंपल रेट के इस्तेमाल किए जा सकने वाले
कॉम्बिनेशन की जांच करने के लिए, AudioProfile (एपीआई लेवल 31 और इसके बाद के लेवल) या
isDirectPlaybackSupported() (एपीआई लेवल 29 और इसके बाद के लेवल) का इस्तेमाल करें.
कुछ Android डिवाइस, आउटपुट ऑडियो डिवाइस के साथ काम करने वाली एन्कोडिंग के अलावा भी एन्कोडिंग इस्तेमाल कर सकते हैं. isDirectPlaybackSupported() की मदद से, इन अतिरिक्त फ़ॉर्मैट का पता लगाया जाना चाहिए. इन मामलों में, ऑडियो डेटा को ऐसे फ़ॉर्मैट में फिर से एन्कोड किया जाता है जो आउटपुट ऑडियो डिवाइस के साथ काम करता है. चुने गए फ़ॉर्मैट के साथ काम करने की सुविधा की सही तरीके से जांच करने के लिए, isDirectPlaybackSupported() का इस्तेमाल करें. भले ही, यह getEncodings() से मिले नतीजों की सूची में मौजूद न हो.
समय से पहले ऑडियो रूट
Android 13 (एपीआई लेवल 33) में, समय से पहले ऑडियो रूट की सुविधा जोड़ी गई है. डिवाइस के ऑडियो एट्रिब्यूट के साथ काम करने की सुविधा का अनुमान लगाया जा सकता है. साथ ही, चालू ऑडियो डिवाइस के लिए ट्रैक तैयार किए जा सकते हैं. यह देखने के लिए कि दिए गए
फ़ॉर्मैट और एट्रिब्यूट के लिए, फ़िलहाल राउट किए गए ऑडियो डिवाइस पर डायरेक्ट प्लेबैक की सुविधा काम करती है या नहीं, getDirectPlaybackSupport() का इस्तेमाल किया जा सकता है:
Kotlin
val format = AudioFormat.Builder() .setEncoding(AudioFormat.ENCODING_E_AC3) .setChannelMask(AudioFormat.CHANNEL_OUT_5POINT1) .setSampleRate(48000) .build() val attributes = AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA) .build() if (AudioManager.getDirectPlaybackSupport(format, attributes) != AudioManager.DIRECT_PLAYBACK_NOT_SUPPORTED ) { // The format and attributes are supported for direct playback // on the currently active routed audio path } else { // The format and attributes are NOT supported for direct playback // on the currently active routed audio path }
Java
AudioFormat format = new AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_E_AC3)
.setChannelMask(AudioFormat.CHANNEL_OUT_5POINT1)
.setSampleRate(48000)
.build();
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.build();
if (AudioManager.getDirectPlaybackSupport(format, attributes) !=
AudioManager.DIRECT_PLAYBACK_NOT_SUPPORTED) {
// The format and attributes are supported for direct playback
// on the currently active routed audio path
} else {
// The format and attributes are NOT supported for direct playback
// on the currently active routed audio path
}
इसके अलावा, यह क्वेरी की जा सकती है कि फ़िलहाल राउट किए गए ऑडियो डिवाइस से, डायरेक्ट मीडिया प्लेबैक के लिए कौनसी प्रोफ़ाइल इस्तेमाल की जा सकती हैं. इसमें वे प्रोफ़ाइल शामिल नहीं होती हैं जो इस्तेमाल नहीं की जा सकती हैं. उदाहरण के लिए, Android फ़्रेमवर्क से ट्रांसकोड की जाने वाली प्रोफ़ाइल:
Kotlin
private fun findBestAudioFormat(audioAttributes: AudioAttributes): AudioFormat { val preferredFormats = listOf( AudioFormat.ENCODING_E_AC3, AudioFormat.ENCODING_AC3, AudioFormat.ENCODING_PCM_16BIT, AudioFormat.ENCODING_DEFAULT ) val audioProfiles = audioManager.getDirectProfilesForAttributes(audioAttributes) val bestAudioProfile = preferredFormats.firstNotNullOf { format -> audioProfiles.firstOrNull { it.format == format } } val sampleRate = findBestSampleRate(bestAudioProfile) val channelMask = findBestChannelMask(bestAudioProfile) return AudioFormat.Builder() .setEncoding(bestAudioProfile.format) .setSampleRate(sampleRate) .setChannelMask(channelMask) .build() }
Java
private AudioFormat findBestAudioFormat(AudioAttributes audioAttributes) {
Stream<Integer> preferredFormats = Stream.<Integer>builder()
.add(AudioFormat.ENCODING_E_AC3)
.add(AudioFormat.ENCODING_AC3)
.add(AudioFormat.ENCODING_PCM_16BIT)
.add(AudioFormat.ENCODING_DEFAULT)
.build();
Stream<AudioProfile> audioProfiles =
audioManager.getDirectProfilesForAttributes(audioAttributes).stream();
AudioProfile bestAudioProfile = (AudioProfile) preferredFormats.map(format ->
audioProfiles.filter(profile -> profile.getFormat() == format)
.findFirst()
.orElseThrow(NoSuchElementException::new)
);
Integer sampleRate = findBestSampleRate(bestAudioProfile);
Integer channelMask = findBestChannelMask(bestAudioProfile);
return new AudioFormat.Builder()
.setEncoding(bestAudioProfile.getFormat())
.setSampleRate(sampleRate)
.setChannelMask(channelMask)
.build();
}
इस उदाहरण में, preferredFormats इंस्टेंस की सूची है AudioFormat.
इसे सूची में सबसे ज़्यादा प्राथमिकता वाले फ़ॉर्मैट से लेकर सबसे कम प्राथमिकता वाले फ़ॉर्मैट के क्रम में लगाया जाता है. getDirectProfilesForAttributes(), दिए गए
AudioAttributes के साथ, फ़िलहाल राउट किए गए ऑडियो डिवाइस के लिए, इस्तेमाल किए जा सकने वाले
AudioProfile ऑब्जेक्ट की सूची दिखाता है. इस्तेमाल किए जा सकने वाले AudioProfile के मैच होने तक, प्राथमिकता वाले AudioFormat आइटम की सूची को दोहराया जाता है. यह
AudioProfile को bestAudioProfile के तौर पर सेव किया जाता है. bestAudioProfile से, सबसे बेहतर सैंपल रेट और चैनल मास्क तय किए जाते हैं. आखिर में, सही
AudioFormat इंस्टेंस बनाया जाता है.
ऑडियो ट्रैक बनाना
ऐप्लिकेशन को इस जानकारी का इस्तेमाल करके, डिफ़ॉल्ट ऑडियो डिवाइस के साथ काम करने वाले सबसे बेहतर AudioFormat के लिए AudioTrack बनाना चाहिए. साथ ही, यह जानकारी चुने गए कॉन्टेंट के लिए उपलब्ध होनी चाहिए.
ऑडियो डिवाइस में होने वाले बदलावों को रोकना
ऑडियो डिवाइस में होने वाले बदलावों को रोकने और उन पर कार्रवाई करने के लिए, ऐप्लिकेशन को ये काम करने चाहिए:
- एपीआई लेवल 24 या इसके बाद के लेवल के लिए, ऑडियो डिवाइस में होने वाले बदलावों (एचडीएमआई,
ब्लूटूथ वगैरह) की निगरानी करने के लिए,
OnRoutingChangedListenerजोड़ें. - एपीआई लेवल 23 के लिए, उपलब्ध ऑडियो डिवाइस की सूची में होने वाले बदलावों की जानकारी पाने के लिए,
AudioDeviceCallbackरजिस्टर करें. - एपीआई लेवल 21 और 22 के लिए, एचडीएमआई प्लग इवेंट की निगरानी करें और ब्रॉडकास्ट से मिले अतिरिक्त डेटा का इस्तेमाल करें.
- एपीआई लेवल 23 से पहले के लेवल वाले डिवाइसों के लिए,
BroadcastReceiverकी स्थिति में होने वाले बदलावों की निगरानी करने के लिए,BluetoothDeviceभी रजिस्टर करें. ऐसा इसलिए, क्योंकिAudioDeviceCallbackकी सुविधा अभी उपलब्ध नहीं है.
AudioTrack के लिए, ऑडियो डिवाइस में होने वाले बदलाव का पता चलने पर, ऐप्लिकेशन को ऑडियो की अपडेट की गई सुविधाओं की जांच करनी चाहिए. साथ ही, ज़रूरत पड़ने पर, किसी दूसरे AudioFormat के साथ AudioTrack को फिर से बनाना चाहिए. ऐसा तब करें, जब अब बेहतर क्वालिटी वाली एन्कोडिंग इस्तेमाल की जा सकती हो या पहले इस्तेमाल की जा रही एन्कोडिंग अब इस्तेमाल नहीं की जा सकती हो.
सैंपल कोड
Kotlin
// audioPlayer is a wrapper around an AudioTrack // which calls a callback for an AudioTrack write error audioPlayer.addAudioTrackWriteErrorListener { // error code can be checked here, // in case of write error try to recreate the audio track restartAudioTrack(findDefaultAudioDeviceInfo()) } audioPlayer.audioTrack.addOnRoutingChangedListener({ audioRouting -> audioRouting?.routedDevice?.let { audioDeviceInfo -> // use the updated audio routed device to determine // what audio format should be used if (needsAudioFormatChange(audioDeviceInfo)) { restartAudioTrack(audioDeviceInfo) } } }, handler)
Java
// audioPlayer is a wrapper around an AudioTrack
// which calls a callback for an AudioTrack write error
audioPlayer.addAudioTrackWriteErrorListener(new AudioTrackPlayer.AudioTrackWriteError() {
@Override
public void audioTrackWriteError(int errorCode) {
// error code can be checked here,
// in case of write error try to recreate the audio track
restartAudioTrack(findDefaultAudioDeviceInfo());
}
});
audioPlayer.getAudioTrack().addOnRoutingChangedListener(new AudioRouting.OnRoutingChangedListener() {
@Override
public void onRoutingChanged(AudioRouting audioRouting) {
if (audioRouting != null && audioRouting.getRoutedDevice() != null) {
AudioDeviceInfo audioDeviceInfo = audioRouting.getRoutedDevice();
// use the updated audio routed device to determine
// what audio format should be used
if (needsAudioFormatChange(audioDeviceInfo)) {
restartAudioTrack(audioDeviceInfo);
}
}
}
}, handler);