Android TV デバイスは、複数のオーディオ出力(テレビスピーカー、HDMI で接続したホームシネマ、Bluetooth ヘッドフォンなど)を同時に接続できます。これらのオーディオ出力デバイスは、エンコード(ドルビー デジタル+、DTS、PCM)、サンプルレート、チャンネルなど、さまざまなオーディオ機能をサポートできます。たとえば、HDMI で接続されたテレビは複数のエンコードをサポートしていますが、接続された Bluetooth ヘッドフォンは通常、PCM のみをサポートしています。
また、使用可能なオーディオ デバイスとルーティングされたオーディオ デバイスは、HDMI デバイスのホットプラグや、Bluetooth ヘッドフォンの接続や接続解除、ユーザーによるオーディオ設定の変更によっても変更できます。アプリがメディアを再生している場合でも音声出力機能は変更される可能性があるため、アプリはこうした変更に適切に適応し、新たにルーティングされたオーディオ デバイスとその機能で再生を継続する必要があります。誤った音声形式を出力すると、エラーや音声が再生されないことがあります。
アプリには、同じコンテンツを複数のエンコードで出力する機能があります。これにより、オーディオ デバイスの機能に応じて最適なオーディオ エクスペリエンスを実現できます。たとえば、テレビでサポートされている場合はドルビー デジタルでエンコードされた音声ストリームが再生され、ドルビー デジタルに対応していない場合は、より広くサポートされている PCM オーディオ ストリームが選択されます。オーディオ ストリームを PCM に変換するために使用される組み込みの Android デコーダのリストについては、サポートされているメディア形式をご覧ください。
再生時に、ストリーミング アプリは、出力オーディオ機器でサポートされている最適な AudioFormat
を使用して AudioTrack
を作成する必要があります。
適切なフォーマットでトラックを作成する
アプリは、AudioTrack
を作成して再生を開始し、getRoutedDevice()
を呼び出して、サウンドを再生するデフォルトのオーディオ デバイスを決定する必要があります。たとえば、ルーティングされたデバイスとそのオーディオ機能の特定にのみ使用される、無音の安全な PCM エンコード トラックなどが該当します。
サポートされているエンコードを取得する
getAudioProfiles()
(API レベル 31 以降)または getEncodings()
(API レベル 23 以降)を使用して、デフォルトのオーディオ デバイスで使用可能なオーディオ形式を特定します。
サポートされている音声プロファイルと形式を確認する
AudioProfile
(API レベル 31 以降)または isDirectPlaybackSupported()
(API レベル 29 以降)を使用して、サポートされている形式、チャンネル数、サンプルレートの組み合わせを確認します。
一部の Android デバイスは、出力オーディオ機器がサポートしていないエンコードをサポートできます。これらの追加の形式は、isDirectPlaybackSupported()
で検出する必要があります。この場合、音声データは出力オーディオ機器でサポートされている形式に再エンコードされます。isDirectPlaybackSupported()
を使用すると、getEncodings()
から返されるリストに目的の形式が存在しない場合でも、その形式のサポートを適切に確認できます。
予測音声ルート
Android 13(API レベル 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
オブジェクトのリストを返します。優先される AudioFormat
アイテムのリストは、サポートされている AudioProfile
が一致するまで反復処理されます。この AudioProfile
は bestAudioProfile
として保存されます。最適なサンプルレートとチャンネル マスクは bestAudioProfile
から決定されます。最後に、適切な AudioFormat
インスタンスが作成されます。
音声トラックを作成
アプリはこの情報を使用して、デフォルトのオーディオ機器でサポートされている(および選択したコンテンツで利用可能な)最高品質の AudioFormat
の AudioTrack
を作成する必要があります。
オーディオ機器の変更をインターセプトする
オーディオ機器の変更をインターセプトして反応するには、アプリは次のことを行う必要があります。
- API レベルが 24 以上の場合は、
OnRoutingChangedListener
を追加して、オーディオ デバイス(HDMI、Bluetooth など)の変更をモニタリングします。 - API レベル 23 の場合、
AudioDeviceCallback
を登録して、使用可能なオーディオ デバイスリストの変更を受け取ります。 - API レベル 21 および 22 の場合、HDMI プラグイベントをモニタリングし、ブロードキャストの追加データを使用します。
- また、API 23 より前のデバイスの
BluetoothDevice
の状態変化をモニタリングするには、BroadcastReceiver
を登録します。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);