播放视频时,调节声音和屏幕亮度是标配啦,简单记录下:
//不要忘记添加权限 :<uses-permission Android:name="android.permission.WRITE_SETTINGS"/>
public class TestActivity extends Activity {
private SeekBar voice_seekbar;
private SeekBar light_seekbar;
private TextView voice_tv;
private TextView light_tv;
private AudioManager mAudioManager;
// 最大音量
private int maxVolume;
// 当前音量
private int currentVolume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
initViews();
initVolume();
initLight();
}
/**
* 获取当前屏幕亮度
*
* @description:
* @author ldm
* @date 2016-12-2 下午3:22:20
*/
private void initLight() {
float currentBright = 0.0f;
try {
// 系统亮度值范围:0~255,应用窗口亮度范围:0.0f~1.0f。
currentBright = android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS) * 100;
} catch (Exception e) {
e.printStackTrace();
}
light_seekbar.setProgress((int) currentBright);
// 转换成百分比
light_tv.setText("当前亮度:" + (int) currentBright + "%");
}
private void initViews() {
this.voice_seekbar = (SeekBar) findViewById(R.id.voice_seekbar);
this.light_seekbar = (SeekBar) findViewById(R.id.light_seekbar);
this.voice_tv = (TextView) findViewById(R.id.voice_tv);
this.light_tv = (TextView) findViewById(R.id.light_tv);
this.voice_seekbar
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
// 设置音量
mAudioManager.setStreamVolume(
AudioManager.STREAM_MUSIC, progress, 0);
voice_tv.setText("当前音量百分比:" + progress * 100
/ maxVolume + " %");
}
});
// 调节亮度
this.light_seekbar
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
WindowManager.LayoutParams lp = getWindow()
.getAttributes();
lp.screenBrightness = Float.valueOf(progress)
* (1f / 100f);
// 调节亮度
getWindow().setAttributes(lp);
light_tv.setText("当前亮度:" + progress + "%");
}
});
}
/**
* 初始化音量数据
*
* @description:
* @author ldm
* @date 2016-12-2 下午3:20:05
*/
private void initVolume() {
// 获取系统最大音量
maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// 设置voice_seekbar的最大值
voice_seekbar.setMax(maxVolume);
// 获取到当前 设备的音量
currentVolume = mAudioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
// 显示音量
voice_tv.setText("当前音量百分比:" + currentVolume * 100 / maxVolume + " %");
}
}
测试用布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:orientation="vertical"
android:padding="16dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="调节声音"
android:textSize="16sp" />
<SeekBar
android:id="@+id/voice_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="调节亮度"
android:textSize="16sp" />
<SeekBar
android:id="@+id/light_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100" />
<TextView
android:id="@+id/voice_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="16sp" />
<TextView
android:id="@+id/light_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="16sp" />
</LinearLayout>