Android音乐播放模式切换-外放、听筒、耳机
2017-02-14 本文已影响3401人
飞奔的蚂蚁
初始化音频管理器
/**
* 初始化音频管理器
*/
private void initAudioManager() {
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
} else {
audioManager.setMode(AudioManager.MODE_IN_CALL);
}
audioManager.setSpeakerphoneOn(true); //默认为扬声器播放
}
切换到听筒模式
/**
* 切换到听筒模式
*/
audioManager.setSpeakerphoneOn(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
audioManager.getStreamMaxVolume(AudioManager.MODE_IN_COMMUNICATION), AudioManager.FX_KEY_CLICK);
} else {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
audioManager.getStreamMaxVolume(AudioManager.MODE_IN_CALL), AudioManager.FX_KEY_CLICK);
}
切换到耳机模式
/**
* 切换到耳机模式
*/
public void changeToHeadsetMode(){
currentMode = MODE_HEADSET;
audioManager.setSpeakerphoneOn(false);
}
切换到外放模式
/**
* 切换到外放模式
*/
public void changeToSpeakerMode(){
currentMode = MODE_SPEAKER;
audioManager.setSpeakerphoneOn(true);
}
设置音量
音量+/-按钮触发
private void setVolume(Context context,boolean upVolume){
int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
if (upVolume) {
volume+=1;
}else{
volume-=1;
}
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
if(volume>=0 && volume<=maxVolume){
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, FLAG_SHOW_UI);
LogUtils.e(String.format("%s->volume:%d,maxVolume:%d",(upVolume?"upVolume":"downVolume"),volume,maxVolume));
}
}
FLAG_SHOW_UI 控制是否显示显示音量大小的ui弹框
拦截系统的音量+/-按钮
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
LogUtils.e("onKeyDown->KEYCODE_VOLUME_DOWN");
SystemUtil.downVolume(this);
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
LogUtils.e("onKeyDown->KEYCODE_VOLUME_UP");
SystemUtil.upVolume(this);
return true;
} else return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
LogUtils.e("onKeyUp->KEYCODE_VOLUME_DOWN");
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
LogUtils.e("onKeyUp->KEYCODE_VOLUME_UP");
return true;
} else return super.onKeyUp(keyCode, event);
}
控制音量状态写成了一个单例类
/**
* 音乐播放管理类
* Created by Administrator on 2015/8/27 0027.
*/
public class PlayerManager {
/**
* 外放模式
*/
public static final int MODE_SPEAKER = 0;
/**
* 耳机模式
*/
public static final int MODE_HEADSET = 1;
/**
* 听筒模式
*/
public static final int MODE_EARPIECE = 2;
private static PlayerManager playerManager;
private AudioManager audioManager;
private Context context;
private int currentMode = MODE_SPEAKER;
public static PlayerManager getManager(){
if (playerManager == null){
synchronized (PlayerManager.class){
playerManager = new PlayerManager();
}
}
return playerManager;
}
public void init(Context context){
this.context = context;
initAudioManager();
}
/**
* 初始化音频管理器
*/
private void initAudioManager() {
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
} else {
audioManager.setMode(AudioManager.MODE_IN_CALL);
}
audioManager.setSpeakerphoneOn(true); //默认为扬声器播放
}
/**
* 获取当前播放模式
* @return
*/
public int getCurrentMode() {
return currentMode;
}
/**
* 切换到听筒模式
*/
public void changeToEarpieceMode(){
currentMode = MODE_EARPIECE;
audioManager.setSpeakerphoneOn(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
audioManager.getStreamMaxVolume(AudioManager.MODE_IN_COMMUNICATION), AudioManager.FX_KEY_CLICK);
} else {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
audioManager.getStreamMaxVolume(AudioManager.MODE_IN_CALL), AudioManager.FX_KEY_CLICK);
}
}
/**
* 切换到耳机模式
*/
public void changeToHeadsetMode(){
currentMode = MODE_HEADSET;
audioManager.setSpeakerphoneOn(false);
}
/**
* 切换到外放模式
*/
public void changeToSpeakerMode(){
currentMode = MODE_SPEAKER;
audioManager.setSpeakerphoneOn(true);
}
/**
* 调大音量
*/
public void raiseVolume(){
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
if (currentVolume < audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)) {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FX_FOCUS_NAVIGATION_UP);
}
}
/**
* 调小音量
*/
public void lowerVolume(){
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
if (currentVolume > 0) {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FX_FOCUS_NAVIGATION_UP);
}
}
public void raiseVolume(Context context){
setVolume(context,true);
}
public void lowerVolume(Context context){
setVolume(context,false);
}
private void setVolume(Context context,boolean upVolume){
int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
if (upVolume) {
volume+=1;
}else{
volume-=1;
}
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
if(volume>=0 && volume<=maxVolume){
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, FLAG_SHOW_UI);
LogUtils.e(String.format("%s->volume:%d,maxVolume:%d",(upVolume?"upVolume":"downVolume"),volume,maxVolume));
}
}
}
监听耳机插入拔出的状态监听
class HeadsetReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action){
//插入和拔出耳机会触发此广播
case Intent.ACTION_HEADSET_PLUG:
addHint(Intent.ACTION_HEADSET_PLUG);
int state = intent.getIntExtra("state", 0);
if (state == 1){
//耳机已插入
playerManager.changeToHeadsetMode();
} else if (state == 0){
//耳机已拔出
}
break;
}
}
}