Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 520|回复: 0

开发交流:Android2.1 架构解析声音

[复制链接]

该用户从未签到

发表于 2011-10-24 09:29:39 | 显示全部楼层 |阅读模式
  10.1 音频管理
       文件:frameworks/base/media/java/android/media/AudioManager.java
       10.1.1 音量控制

java代码: /** @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

};
复制代码以上结构定义了默认音量。

java代码: 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 播放控制

java代码: @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);

}
复制代码10.2 音频服务

        文件:frameworks/base/media/java/android/media/AudioService.java

       10.2.1 音效

java代码: 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 音量

java代码: 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(stream, (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 音频类型

java代码: * 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 输入输出设备

java代码: // 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++文件进行处理。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2025-1-10 19:08 , Processed in 0.393717 second(s), 46 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表