背景
客户机器默认的开机声音一直很大;客户觉得无法接受,需要改小点。
基于Android 7的代码
前言
一般主要通过系统层来进行修改。
在系统关于音频的有关代码中,定义了两个数组:
注意,这些代码根据版本的不同而不同,但都能在
frameworks/base/media/java/android/media
中找到。
虽然我没有找到 定义音量最大值的入口,但是可以看看:https://blog.csdn.net/xuechwtx/article/details/80868996
MAX_STREAM_VOLUME
:定义了各种声音的最大值。
注意:最大值不是100,需要
AudioManager.getStreamMaxVolume(type)
来获取各个音量的最大值,然后进行设置。
private static int[] MAX_STREAM_VOLUME = new int[] {
5,
7, // STREAM_SYSTEMX_STREAM_VOLUMEMAX_STREAM_VOLUMEMAX_STREAM_VOLUME
7, // STREAM_RING
15, // STREAM_MUSIC
7, // STREAM_ALARM
7, // STREAM_NOTIFICATION
15, // STREAM_BLUETOOTH_SCO
7, // STREAM_SYSTEM_ENFORCED
15, // STREAM_DTMF
15 // STREAM_TTS
};
DEFAULT_STREAM_VOLUME
:分别对应各种声音的默认的大小(顺序与MAX_STREAM_VOLUME
相同)。
public class AudioSystem
{
private static final String TAG = "AudioSystem";
/* These values must be kept in sync with system/audio.h */
/*
* If these are modified, please also update Settings.System.VOLUME_SETTINGS
* and attrs.xml and AudioManager.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;
// ...
public static final String[] STREAM_NAMES = new String[] {
"STREAM_VOICE_CALL",
"STREAM_SYSTEM",
"STREAM_RING",
"STREAM_MUSIC",
"STREAM_ALARM",
"STREAM_NOTIFICATION",
"STREAM_BLUETOOTH_SCO",
"STREAM_SYSTEM_ENFORCED",
"STREAM_DTMF",
"STREAM_TTS"
};
// ...
private 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
};
// ...
}
如果需要修改默认的通知声音,则可以将对应的数值 改为新的值即可。
MIN:0为静音;MAX:每种音量都不一样。
思路很简单,只需要将对应的DEFAULT_STREAM_VOLUME
改一下即可。
如何知道所要改的声音是属于哪种 type?这个可以查看有关的源码:
// frameworks/base/cmds/bootanimation/BootAnimation.cpp
void BootAnimation::bootMusic()
{
int index;
const char *fileName = "/system/media/boot.wav";
MediaPlayer* mp = new MediaPlayer();
// 从这里我们可以知道,是STREAM_SYSTEM_ENFORCED类型的
audio_devices_t device = AudioSystem::getDevicesForStream(AUDIO_STREAM_ENFORCED_AUDIBLE);
if (mp->setDataSource(NULL, fileName, NULL) == NO_ERROR)
{
mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE/*AudioSystem: :ENFORCED_AUDIBLE*/);
mp->prepare();
}
LOGE ("bootMusic
");
AudioSystem::initStreamVolume(AUDIO_STREAM_ENFORCED_AUDIBLE, 0,7);
AudioSystem::setStreamVolumeIndex(AUDIO_STREAM_ENFORCED_AUDIBLE, 7, device);
AudioSystem::getStreamVolumeIndex(AUDIO_STREAM_ENFORCED_AUDIBLE/*AudioSystem::ENFORCED_AUDIBLE*/, &index, device);
LOGE ("index %d",index);
if (index != 0)
{
LOGD("playing %s", fileName);
mp->setVolume(0.4f, 0.4f); // 调整左右声道的声音
mp->seekTo(0);
mp->start();
}
}