2017年5月6日 星期六

Android Audio 心得筆記(一):Audio Stream類型與預設音量

因為一些工作關係所以接觸到Android上層Audio Framework與Volume相關的部分,
記錄一下這一段時間追Code的心得,這邊會以Google N的Code為參考.

Android Audio總共定義10種STREAM類型(不含DEFAULT),
用途都蠻明確的,像是講電話相關的就是使用STREAM_VOICE_CALL,
鈴聲部分則是STREAM_RING,訊息等通知音效則為STREAM_NOTIFICATION,
諸如此類...這些都定義在framework的AudioSystem.java中。

/* The default audio stream */ 
public static final int STREAM_DEFAULT = -1; 
/* The audio stream for phone calls */ 
public static final int STREAM_VOICE_CALL = 0; 
/* The audio stream for system sounds */ 
public static final int STREAM_SYSTEM = 1; 
/* The audio stream for the phone ring and message alerts */ 
public static final int STREAM_RING = 2; 
/* The audio stream for music playback */ 
public static final int STREAM_MUSIC = 3; 
/* The audio stream for alarms */ 
public static final int STREAM_ALARM = 4; 
/* The audio stream for notifications */ 
public static final int STREAM_NOTIFICATION = 5; 
/* @hide The audio stream for phone calls when connected on bluetooth */ 
public static final int STREAM_BLUETOOTH_SCO = 6; 
/* @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */ 
public static final int STREAM_SYSTEM_ENFORCED = 7; 
/* @hide The audio stream for DTMF tones */ 
public static final int STREAM_DTMF = 8; 
/* @hide The audio stream for text to speech (TTS) */ 
public static final int STREAM_TTS = 9; 
不過因為AudioSystem這個class是@hide,所以一般App層主要是透過AudioManager來使用這些STREAM,
AudioManager.java中各STREAM定義如下:
/** The audio stream for phone calls */ 
public static final int STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL; 
/** The audio stream for system sounds */ 
public static final int STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM; 
/** The audio stream for the phone ring */ 
public static final int STREAM_RING = AudioSystem.STREAM_RING; 
/** The audio stream for music playback */ 
public static final int STREAM_MUSIC = AudioSystem.STREAM_MUSIC; 
/** The audio stream for alarms */ 
public static final int STREAM_ALARM = AudioSystem.STREAM_ALARM; 
/** The audio stream for notifications */ 
public static final int STREAM_NOTIFICATION = AudioSystem.STREAM_NOTIFICATION; 
/** @hide The audio stream for phone calls when connected to bluetooth */ 
public static final int STREAM_BLUETOOTH_SCO = AudioSystem.STREAM_BLUETOOTH_SCO; 
/** @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */ 
public static final int STREAM_SYSTEM_ENFORCED = AudioSystem.STREAM_SYSTEM_ENFORCED; 
/** The audio stream for DTMF Tones */ 
public static final int STREAM_DTMF = AudioSystem.STREAM_DTMF; 
/** @hide The audio stream for text to speech (TTS) */ 
public static final int STREAM_TTS = AudioSystem.STREAM_TTS; 
其實就只是單純的參考到AudioSystem而已,
這些STREAM的預設音量則是在AudioSystem.java定義:
public static int[] DEFAULT_STREAM_VOLUME = new int[] { 
    4,  // STREAM_VOICE_CALL 
    7,  // STREAM_SYSTEM 
    5,  // STREAM_RING 
    11, // STREAM_MUSIC 
    6,  // STREAM_ALARM 
    5,  // STREAM_NOTIFICATION 
    7,  // STREAM_BLUETOOTH_SCO 
    7,  // STREAM_SYSTEM_ENFORCED 
    11, // STREAM_DTMF 
    11  // STREAM_TTS 
};
從Code來看相同STREAM不同輸出設備(ex:Speaker、Headset、BLUETOOTH等)的預設值都是一樣的,
並沒有針對特定的輸出設備去做額外的預設值設定,
不過在MUSIC這個STREAM與Headset/Headphone這部分比較特別,
因為考慮使用者插入耳機聽音樂時,預設音量可能會太大聲造成耳朵傷害,
Android這邊有額外做了保護機制(Safe_Medua_Volume)的判斷,
如果預設音量超出這個值的話,
則會自動將其音量修改為Safe Media Volume的值(default 10),
當然使用者可以透過音量鍵的的調整,
自行這時候的音量值調大,不過此時SystemUI會先跳出一個警告訊息,
告知使用者如果繼續往上調,其音量可能會傷害耳朵,
如果使用者允許才能繼續往上調整,
如果不允許就會回到Safe Media Volume的狀態。

沒有留言:

張貼留言

Android Audio 心得筆記(一):Audio Stream類型與預設音量

因為一些工作關係所以接觸到Android上層Audio Framework與Volume相關的部分, 記錄一下這一段時間追Code的心得,這邊會以Google N的Code為參考. Android Audio總共定義10種STREAM類型(不含DEFAULT), 用途都蠻...