设置音量的方法也很简单,AudioManager提供了方法:
publicvoidsetStreamVolume(intstreamType,intindex,intflags)其中streamType有内置的常量,去文档里面就可以看到。
JAVA代码:
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//通话音量
int max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_VOICE_CALL );
int current = mAudioManager.getStreamVolume( AudioManager.STREAM_VOICE_CALL );
Log.d(“VIOCE_CALL”, “max : ” + max + ” current : ” + current);
//系统音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_SYSTEM );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_SYSTEM );
Log.d(“SYSTEM”, “max : ” + max + ” current : ” + current);
//铃声音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_RING );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_RING );
Log.d(“RING”, “max : ” + max + ” current : ” + current);
//音乐音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_MUSIC );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );
Log.d(“MUSIC”, “max : ” + max + ” current : ” + current);
//提示声音音量
max = mAudioManager.getStreamMaxVolume( AudioManager.STREAM_ALARM );
current = mAudioManager.getStreamVolume( AudioManager.STREAM_ALARM );
Log.d(“ALARM”, “max : ” + max + ” current : ” + current);
ps:
游戏过程中只允许调整多媒体音量,而不允许调整通话音量。
setVolumeControlStream(AudioManager.STREAM_MUSIC);
长时间不动,不允许黑屏,View.setKeepScreenOn(true);
估计manifest文件中需要注册权限吧
a、通过系统服务获得声音管理器:
AudioManager audioManager = (AudioManager)getSystemService(Service.AUDIO_SERVICE);
b、根据实际需要调用适当的方法:(常用方法)
audioManager.adjustStreamVolume(int streamType, int direction, int flags);
streamType:声音类型,可取的为STREAM_VOICE_CALL(打电话时的声音), STREAM_SYSTEM(Android系统声音), STREAM_RING(电话铃响), STREAM_MUSIC(音乐声音) or STREAM_ALARM(警告声音)。
direction:调整音量的方向,可取为ADJUST_LOWER(调低音量), ADJUST_RAISE(调高音量), or ADJUST_SAME(保持先前音量)。
flags:可选标志位(如要显示出音量调节UI,使用如下flag:AudioManager.FLAG_SHOW_UI)。
audioManager.setStreamMute(int streamType, boolean state);设置指定声音类型(streamType)是否为静音。如果state为true,则设置为静音;否则,不设置为静音。
audioManager.setRingerMode(int ringerMode);
设置铃音模式,可取值为RINGER_MODE_NORMAL(铃音正常模式), RINGER_MODE_SILENT(铃音静音模式), or RINGER_MODE_VIBRATE(铃音震动模式,即铃音为静音,启动震动)。
audioManager.setMode(int mode);
设置声音模式,可取值为MODE_NORMAL(正常模式,即在没有铃音与电话的情况), MODE_RINGTONE(铃响模式), MODE_IN_CALL(接通电话模式) or MODE_IN_COMMUNICATION(通话模式)。
注意:声音的调节是没有权限要求的。
mSoundProgress = (ProgressBar) v.findViewById(R.id.sound_seekbar);
-----------------------------------------------------------------------------------------------------------------------------------------
<SeekBar
android:id="@+id/sound_seekbar"
style="@style/Controller_Sound_SeekBar"
----------------------------------------------------------------
<style name="Controller_Sound_SeekBar">
<item name="android:minHeight">97dip</item>
<item name="android:maxHeight">97dip</item>
<item name="android:progressDrawable">@drawable/controller_sound_seekbar</item>
--------------------------------
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background" android:drawable="@drawable/sound_seekbar_1"/>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="#00000000" />
</shape>
</clip>
</item>
<item>
<layer-list>
<item
android:id="@android:id/progress"
android:drawable="@drawable/sound_seekbar_2">
</item>
</layer-list>
</item>
</layer-list>
-------------------------------
</style>
</resources>
----------------------------------------------------------------
android:layout_width="100dip"
android:layout_height="40dip"
android:layout_marginLeft="15dip"
android:layout_marginTop="35dip"
android:layout_toRightOf="@id/btn_sound"
android:thumb="@null"
android:focusable="true" />
-----------------------------------------------------------------------------------------------------------------------------------------
if (mSoundProgress != null) {
int maxVolume = mAM.getStreamMaxVolume(AudioManager.STREAM_MUSIC);//最大音量
int currentVolume = mAM.getStreamVolume(AudioManager.STREAM_MUSIC);//当前音量
if (mSoundProgress instanceof SeekBar) {
SeekBar seeker = (SeekBar) mSoundProgress;
seeker.setOnSeekBarChangeListener(mSoundSeekListener);
------------------------------------------------------------------------------------------------------------------------------------------
private OnSeekBarChangeListener mSoundSeekListener = new OnSeekBarChangeListener() {
public void onStartTrackingTouch(SeekBar bar) {
}
public void onProgressChanged(SeekBar bar, int progress, boolean fromuser) {
mAM.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0); //拖动seekbar时改变音量
if(progress == 0){
switchSound(true);
return;
}
switchSound(false);
}
public void onStopTrackingTouch(SeekBar bar) {
}
};
------------------------------------------------------------------------------------------------------------------------------------------
}
mSoundProgress.setMax(maxVolume);
mSoundProgress.setProgress(currentVolume);
}
private View.OnClickListener mSoundListener = new View.OnClickListener() {
boolean isSilent = false;
public void onClick(View v) {
if(isSilent){
isSilent = false;
switchSound(false);
return;
}
isSilent = true;
switchSound(true);
}
};
private void switchSound(boolean slient){
if(slient){
mSoundButton.setBackgroundResource(R.drawable.btn_sound_clear);
mAM.setStreamMute(AudioManager.STREAM_MUSIC, true);
return;
}
mSoundButton.setBackgroundResource(R.drawable.btn_sound);
mAM.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
show:
int currentVolume = mAM.getStreamVolume(AudioManager.STREAM_MUSIC);//当前音量
mSoundProgress.setProgress(currentVolume);