ysk-san KT

技術系の情報をKTするために、まずは勉強

android.media.audiopolicy.AudioProductStrategyについて調べた/ I looked up android.media.audiopolicy.AudioProductStrategy

(English below)

android.media.audiopolicy.AudioProductStrategy

android.media.audiopolicy.AudioProductStrategyは、Androidバイス上で音声ポリシーを実装するためのクラスです。AudioProductStrategyは、デバイスに接続された異なる種類のオーディオ製品(ヘッドセット、スピーカー、Bluetoothオーディオデバイスなど)に対して、どの音声ストリームを再生するか、どの音声処理を適用するか、どの音声エフェクトを適用するかなどを制御するために使用されます。

 

AudioProductStrategyは、オーディオポリシーフレームワークの一部であり、異なる種類のオーディオ製品が同時に接続されている場合に、デバイスが正しい音声ストリームを選択し、オーディオの品質を維持することができるようになっています。このクラスを使用すると、アプリケーション開発者は、オーディオ製品の特定の機能を活用し、ユーザーが期待する音声エクスペリエンスを提供することができます。

 

android.media.audiopolicy.AudioProductStrategyは、抽象クラスであるため、直接インスタンス化することはできません。代わりに、このクラスを拡張してカスタムのオーディオポリシーを実装する必要があります。

 

以下は、AudioProductStrategyを拡張したサンプルのコードです。

 

import android.media.audiopolicy.AudioProductStrategy;
import android.media.audiopolicy.AudioVolumeGroup;

public class MyAudioProductStrategy extends AudioProductStrategy {
    @Override
    public void onAudioVolumeGroupChanged(AudioVolumeGroup avg) {
        // 音量グループが変更されたときに呼び出されるコールバック
    }

    @Override
    public void onOutputGainRequest(AudioVolumeGroup avg, int request, int streamType) {
        // 出力ゲインがリクエストされたときに呼び出されるコールバック
    }

    @Override
    public void onOutputDeviceRequest(AudioVolumeGroup avg, int request, int reason) {
        // 出力デバイスがリクエストされたときに呼び出されるコールバック
    }
}

このクラスを使用するには、AudioPolicyを作成して、AudioPolicy.Builderを使用してMyAudioProductStrategyのインスタンスを渡します。以下は、サンプルコードです。

 

import android.media.audiopolicy.AudioPolicy;
import android.media.audiopolicy.AudioPolicyConfig;

AudioPolicyConfig config = new AudioPolicyConfig.Builder(context)
        .setProductStrategies(new MyAudioProductStrategy())
        .build();

AudioPolicy policy = new AudioPolicy.Builder(context)
        .setAudioPolicyConfig(config)
        .build();

このように、AudioProductStrategyを拡張することで、オーディオポリシーをカスタマイズできます。

android.media.audiopolicy.AudioProductStrategyは、Androidフレームワークの一部であり、オープンソースで公開されています。次のリンクから、Android Open Source ProjectのGitリポジトリでこのクラスのソースコードを閲覧できます。

https://android.googlesource.com/platform/frameworks/av/+/refs/heads/master/media/libaudiopolicy/include/media/AudioProductStrategy.h

 

また、このクラスのJavaコードも同様に公開されています。次のリンクから、Android Open Source ProjectのGitリポジトリでAudioProductStrategy.javaソースコードを閲覧できます。

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/media/java/android/media/audiopolicy/AudioProductStrategy.java

 

これらのリンクから、AudioProductStrategyの詳細な実装を確認できます。

 

android.media.audiopolicy.AudioProductStrategyクラスで使用できるいくつかの定数ストラテジーがあります。これらのストラテジーは、オーディオポリシーが各ストラテジーに対してどのように動作するかを定義します。STRATEGY_MEDIA以外のストラテジーには以下のものがあります。

  1. STRATEGY_PHONE: 通話のために使用されるストラテジーで、通話用のスピーカーとマイクを制御します。
  2. STRATEGY_SONIFICATION: システムイベントの音を再生するために使用されるストラテジーです。例えば、着信音、通知音、アラーム音などが含まれます
  3. STRATEGY_ACCESSIBILITY: バイブレーターや音声フィードバックなど、アクセシビリティ機能に必要な音声を制御するために使用されるストラテジーです。

これらのストラテジーは、AudioAttributesクラスのusageフィールドを使用して指定されます。たとえば、以下のようにしてAudioProductStrategyを作成できます。

 
AudioProductStrategy strategy = new AudioProductStrategy.Builder()
        .setProductType(AudioProductStrategy.ProductType.RINGER)
        .setAudioAttributes(new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                .build())
        .build();

この例では、STRATEGY_SONIFICATIONストラテジーが使用されています。AudioAttributesのusageがUSAGE_NOTIFICATION_RINGTONEに設定されているため、通知音の再生に適したストラテジーが自動的に選択されます。

 

■English translation

android.media.audiopolicy.AudioProductStrategy

AudioProductStrategy is a class for implementing audio policy on Android devices. AudioProductStrategy is used to control which audio streams are played, which audio processing is applied, which audio effects are applied, etc., for the different types of audio products (headsets, speakers, Bluetooth audio devices, etc.) connected to the device.

AudioProductStrategy is part of the audio policy framework that allows devices to select the correct audio stream and maintain audio quality when different types of audio products are connected simultaneously. This class allows application developers to take advantage of specific features of audio products to provide the audio experience that users expect.

Since android.media.audiopolicy.AudioProductStrategy is an abstract class, it cannot be instantiated directly. Instead, this class must be extended to implement a custom audio policy.

Below is a sample code that extends AudioProductStrategy.

 

import android.media.audiopolicy.AudioProductStrategy;
import android.media.audiopolicy.AudioVolumeGroup;

public class MyAudioProductStrategy extends AudioProductStrategy {
    @Override
    public void onAudioVolumeGroupChanged(AudioVolumeGroup avg) {
        // 音量グループが変更されたときに呼び出されるコールバック
    }

    @Override
    public void onOutputGainRequest(AudioVolumeGroup avg, int request, int streamType) {
        // 出力ゲインがリクエストされたときに呼び出されるコールバック
    }

    @Override
    public void onOutputDeviceRequest(AudioVolumeGroup avg, int request, int reason) {
        // 出力デバイスがリクエストされたときに呼び出されるコールバック
    }
}

To use this class, create an AudioPolicy and pass an instance of MyAudioProductStrategy using AudioPolicy.Builder. The following is sample code.

 

import android.media.audiopolicy.AudioPolicy;
import android.media.audiopolicy.AudioPolicyConfig;

AudioPolicyConfig config = new AudioPolicyConfig.Builder(context)
        .setProductStrategies(new MyAudioProductStrategy())
        .build();

AudioPolicy policy = new AudioPolicy.Builder(context)
        .setAudioPolicyConfig(config)
        .build();

Thus, you can extend AudioProductStrategy to customize your audio policy.

The android.media.audiopolicy.AudioProductStrategy is part of the Android framework and is open source. You can view the source code for this class in the Android Open Source Project Git repository at the following link

https://android.googlesource.com/platform/frameworks/av/+/refs/heads/master/media/libaudiopolicy/include/media/ AudioProductStrategy.h

 

Java code for this class is also available as well. You can view the source code of AudioProductStrategy.java in the Git repository of the Android Open Source Project at the following link

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/media/java/android/media/audiopolicy/ AudioProductStrategy.java

 

These links will take you to a detailed implementation of AudioProductStrategy.

 

There are several constant strategies available in the android.media.audiopolicy.AudioProductStrategy class. These strategies define how the audio policy will behave for each strategy. strategies other than STRATEGY_MEDIA include.

  1. STRATEGY_PHONE: Strategy used for calls, controlling the speaker and microphone for calls.
  2. STRATEGY_SONIFICATION: Strategy used for playing sounds of system events. Examples include ringtones, notification sounds, alarm sounds, etc.
  3. STRATEGY_ACCESSIBILITY: Strategies used to control the audio required for accessibility features, such as vibrators and voice feedback.

These strategies are specified using the usage field of the AudioAttributes class. For example, an AudioProductStrategy can be created as follows

 
AudioProductStrategy strategy = new AudioProductStrategy.Builder()
        .setProductType(AudioProductStrategy.ProductType.RINGER)
        .setAudioAttributes(new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                .build())
        .build();

In this example, the STRATEGY_SONIFICATION strategy is used: the usage in AudioAttributes is set to USAGE_NOTIFICATION_RINGTONE, so the appropriate strategy for notification sound playback is automatically selected The following is a list of the strategies that are used.