欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

转载 ADROID 2.1 架构解析 10 声音

程序员文章站 2022-06-09 08:00:40
...
10 声音
10.1 音频管理
文件: frameworks/base/media/java/android/media/AudioManager.java

10.1.1 音量控制

/** @hide Default volume index values for audio streams */

public static final 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

};


以上结构定义了默认音量。


public void adjustStreamVolume(int streamType, int direction, int flags) {

IAudioService service = getService();

try {

service.adjustStreamVolume(streamType, direction, flags);

} catch (RemoteException e) {

Log.e(TAG, "Dead object in adjustStreamVolume", e);

}

}

public void setStreamVolume(int streamType, int index, int flags) {

IAudioService service = getService();

try {

service.setStreamVolume(streamType, index, flags);

} catch (RemoteException e) {

Log.e(TAG, "Dead object in setStreamVolume", e);

}

}



将音量控制参数转给音频服务去处理。

10.1.2 播放控制

@Deprecated public void setParameter(String key, String value) {

setParameters(key+"="+value);

}



/**

* Sets a variable number of parameter values to audio hardware.

*

* @param keyValuePairs list of parameters key value pairs in the form:

* key1=value1;key2=value2;...

*

*/

public void setParameters(String keyValuePairs) {

AudioSystem.setParameters(keyValuePairs);

}

cupcake 的播放控制采用的是 setrouting() 方式进行, eclaire 的播放控制改用 setParameter 方式,用参数设置的方式进行。

以上代码的功能是:将参数传给音频系统去进行处理。

10.2 音频服务
文件: frameworks/base/media/java/android/media/AudioService.java

10.2.1 音效
 
private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/";

private static final String[] SOUND_EFFECT_FILES = new String[] {

"Effect_Tick.ogg",

"KeypressStandard.ogg",

"KeypressSpacebar.ogg",

"KeypressDelete.ogg",

"KeypressReturn.ogg"

};

public boolean loadSoundEffects() {

...

String filePath = Environment.getRootDirectory() + SOUND_EFFECTS_PATH + SOUND_EFFECT_FILES[SOUND_EFFECT_FILES_MAP[effect][0]];

...

return true;

}


音效文件放在: /system/media/audio/ui/ 目录下。

10.2.2 音量

public void setStreamVolume(int streamType, int index, int flags) {

ensureValidStreamType(streamType);

index = rescaleIndex(index * 10, streamType, STREAM_VOLUME_ALIAS[streamType]);

setStreamVolumeInt(STREAM_VOLUME_ALIAS[streamType], index, false, true);



// UI, etc.

mVolumePanel.postVolumeChanged(streamType, flags);

// Broadcast Intent

sendVolumeUpdate(streamType);

}

private void setStreamVolumeInt(int streamType, int index, boolean force, boolean lastAudible) {

VolumeStreamState streamState = mStreamStates[streamType];

if (streamState.setIndex(index, lastAudible) || force) {

// Post message to set system volume (it in turn will post a message

// to persist). Do not change volume if stream is muted.

if (streamState.muteCount() == 0) {

sendMsg(mAudioHandler, MSG_SET_SYSTEM_VOLUME, streamType, SENDMSG_NOOP, 0, 0,

streamState, 0);

}

}

}

private class AudioHandler extends Handler {

private void setStreamVolumeIndex(int stream, int index) {

AudioSystem.setStreamVolumeIndex(s tream, (index + 5)/10);

}



private void setSystemVolume(VolumeStreamState streamState) {



// Adjust volume

setStreamVolumeIndex (streamState.mStreamType, streamState.mIndex);

...

}

public void handleMessage(Message msg) {

int baseMsgWhat = getMsgBase(msg.what);



switch (baseMsgWhat) {



case MSG_SET_SYSTEM_VOLUME:

setSystemVolume((VolumeStreamState) msg.obj);

break;

...


音量设置通过消息发给消息处理,然后再转向音频系统进行处理:

AudioSystem.setStreamVolumeIndex

10.3 音频系统
文件: frameworks/base/media/java/android/media/AudioSystem.java

10.3.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;

10.3.2 输入输出设备

// output devices

public static final int DEVICE_OUT_EARPIECE = 0x1;

public static final int DEVICE_OUT_SPEAKER = 0x2;

public static final int DEVICE_OUT_WIRED_HEADSET = 0x4;

public static final int DEVICE_OUT_WIRED_HEADPHONE = 0x8;

public static final int DEVICE_OUT_BLUETOOTH_SCO = 0x10;

public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20;

public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40;

public static final int DEVICE_OUT_BLUETOOTH_A2DP = 0x80;

public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100;

public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200;

public static final int DEVICE_OUT_AUX_DIGITAL = 0x400;

public static final int DEVICE_OUT_DEFAULT = 0x8000;

// input devices

public static final int DEVICE_IN_COMMUNICATION = 0x10000;

public static final int DEVICE_IN_AMBIENT = 0x20000;

public static final int DEVICE_IN_BUILTIN_MIC1 = 0x40000;

public static final int DEVICE_IN_BUILTIN_MIC2 = 0x80000;

public static final int DEVICE_IN_MIC_ARRAY = 0x100000;

public static final int DEVICE_IN_BLUETOOTH_SCO_HEADSET = 0x200000;

public static final int DEVICE_IN_WIRED_HEADSET = 0x400000;

public static final int DEVICE_IN_AUX_DIGITAL = 0x800000;

public static final int DEVICE_IN_DEFAULT = 0x80000000;

10.3.3 转向底层处理

public static native int setParameters(String keyValuePairs);

public static native int setStreamVolumeIndex(int stream, int index);


对于参数设置和音量设置都转向 C/C++ 文件进行处理。

10.4 音频系统底层处理
文件: frameworks/base/media/libmedia/AudioSystem.cpp

10.4.1 音量设置

const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service()

{

...

sp<IServiceManager> sm = defaultServiceManager();

...

binder = sm->getService(String16("media.audio_policy"));

...

}

status_t AudioSystem::setStreamVolumeIndex(stream_type stream, int index)

{

const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();

if (aps == 0) return PERMISSION_DENIED;

return aps->setStreamVolumeIndex(stream, index);

}

将参数传给音频策略进行处理。

10.4.1 参数设置

const sp<IAudioFlinger>& AudioSystem::get_audio_flinger()

{

...

sp<IServiceManager> sm = defaultServiceManager();

..

binder = sm->getService(String16("media.audio_flinger"));

...

}

status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) {

const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();

if (af == 0) return PERMISSION_DENIED;

return af->setParameters(ioHandle, keyValuePairs);

}


将参数传给音频投掷进行处理。

主参数:

const char *AudioParameter::keyRouting = "routing";

const char *AudioParameter::keySamplingRate = "sampling_rate";

const char *AudioParameter::keyFormat = "format";

const char *AudioParameter::keyChannels = "channels";

const char *AudioParameter::keyFrameCount = "frame_count";

10.5 音频策略
文件: frameworks/base/libs/audioflinger/AudioPolicyService.cpp

void AudioPolicyService::instantiate() {

defaultServiceManager()->addService(

String16("media.audio_policy"), new AudioPolicyService());

}


注册名为 media.audio_policy 的服务。

AudioPolicyService::AudioPolicyService()

: BnAudioPolicyService() , mpPolicyManager(NULL)

{

...

mpPolicyManager = createAudioPolicyManager(this);

...

}


调用音频策略管理器。

status_t AudioPolicyService::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)

{

...



return mpPolicyManager->setStreamVolumeIndex(stream, index);

}


将音量控制参数传给音频策略管理器。

10.6 音频投掷
文件: frameworks/base/libs/audioflinger/AudioFlinger.cpp

void AudioFlinger::instantiate() {

defaultServiceManager()->addService(

String16("media.audio_flinger"), new AudioFlinger());

}


注册名为 media.audio_flinger 的服务

AudioFlinger::AudioFlinger()

: BnAudioFlinger(),

mAudioHardware(0), mMasterVolume(1.0f), mMasterMute(false), mNextThreadId(0)

{

...

mAudioHardware = AudioHardwareInterface::create();

...

}

status_t AudioFlinger::setParameters(int ioHandle, const String8& keyValuePairs)

{

...

result = mAudioHardware->setParameters(keyValuePairs);

...

}


将参数转向硬件接口去进行处理。

10.6.1 硬件接口
文件: frameworks/base/libs/audioflinger/AudioHardwareInterface.cpp

AudioHardwareInterface* AudioHardwareInterface::create()

{

...

hw = createAudioHardware();

...

}

调用音频设备。

10.7 音频策略管理器
文件: hardware/msm7k/libaudio/AudioPolicyManager.cpp

extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)

{

return new AudioPolicyManager(clientInterface);

}

status_t AudioPolicyManager::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)

{



...

for (size_t i = 0; i < mOutputs.size(); i++) {

status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), mOutputs.valueAt(i)->device());

if (volStatus != NO_ERROR) {

status = volStatus;

}

}

return status;

}

设置音量。

10.8 音频设备
文件: hardware/msm7k/libaudio/AudioHardware.cpp

extern "C" AudioHardwareInterface* createAudioHardware (void) {

return new AudioHardware();

}

status_t AudioHardware::setParameters(const String8& keyValuePairs)

{

AudioParameter param = AudioParameter(keyValuePairs);

String8 value;

String8 key;

...

key = String8(BT_NAME_KEY);

if (param.get(key, value) == NO_ERROR) {

...

}

return NO_ERROR;

}


处理 setParameters 参数。
相关标签: Java Android UI