80,351
社区成员
发帖
与我相关
我的任务
分享
package com.orietech.seekbardemo;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
/**
* @Copyright (c) 2011-2012 ORIETech
* @Class Name:MainActivity
* @Describe:
* @Author:Jason Zhang
* @Modifier:Jason Zhang
* @version:V1.0
*
*/
public class MainActivity extends Activity {
private AudioManager audiomanage;
private SeekBar seekBar;
//是否在滑动中 true 是滑动中
private boolean isScool=false;
//
int numCount=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
seekBar = (SeekBar) findViewById(R.id.seekbar);
audiomanage = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int max = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
seekBar.setMax(max);
numCount=max;
int currentVolume = audiomanage
.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
seekBar.setProgress(currentVolume);
Log.e(MainActivity.class.getName(), "max=" + max + "---currentVolume="
+ currentVolume);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() // 调音监听器
{
public void onProgressChanged(SeekBar arg0, int progress,
boolean fromUser) {
audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, AudioManager.FLAG_SHOW_UI);
// int currentVolume = audiomanage
// .getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
// seekBar.setProgress(currentVolume);
Log.e(MainActivity.class.getName(), "---progress=" + progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
isScool=true;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
isScool=false;
}
});
IntentFilter filter = new IntentFilter(
"android.media.VOLUME_CHANGED_ACTION");
registerReceiver(receiver, filter);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(receiver);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
int currentVolume = audiomanage
.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
Log.e(MainActivity.class.getName(), "onKeyDown currentVolume="
+ currentVolume);
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
if (event.getRepeatCount() == 0) {
if (currentVolume < numCount) {
currentVolume = currentVolume + 1;
}
audiomanage.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
seekBar.setProgress(currentVolume);
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
if (event.getRepeatCount() == 0) {
if (currentVolume > 0) {
currentVolume = currentVolume - 1;
}
audiomanage.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
seekBar.setProgress(currentVolume);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
/*@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int currentVolume = audiomanage
.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
int action = event.getAction();
if (action ==KeyEvent.ACTION_DOWN) {
if (currentVolume > 0) {
currentVolume = currentVolume - 1;
}
audiomanage.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
seekBar.setProgress(currentVolume);
return true;
}
if (action== KeyEvent.ACTION_UP) {
if (currentVolume < numCount) {
currentVolume = currentVolume + 1;
}
audiomanage.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
seekBar.setProgress(currentVolume);
return true;
}
return super.dispatchKeyEvent(event);
}
*/
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.media.VOLUME_CHANGED_ACTION"
.equals(intent.getAction())&&!isScool) {
int type = intent.getIntExtra(
"android.media.EXTRA_VOLUME_STREAM_TYPE", 0);
int index = intent.getIntExtra(
"android.media.EXTRA_VOLUME_STREAM_VALUE", 0);
int oldIndex = intent.getIntExtra(
"android.media.EXTRA_PREV_VOLUME_STREAM_VALUE", 0);
seekBar.setProgress(index);
Log.e(MainActivity.class.getName(), "type=" + type
+ " index=" + index + " oldIndex=" + oldIndex);
//android.media.VOLUME_CHANGED_ACTION
//Intent intent = new Intent(AudioManager.VOLUME_CHANGED_ACTION);
// android.media.EXTRA_VOLUME_STREAM_TYPE
// intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE,
// streamType);
// android.media.EXTRA_VOLUME_STREAM_VALUE
// intent.putExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE,
// index);
// android.media.EXTRA_PREV_VOLUME_STREAM_VALUE
// intent.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE,
// oldIndex);
}
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<SeekBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</SeekBar>
<VideoView
android:id="@+id/video"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000" />
</LinearLayout>