android 音乐控制工具类
在某些情况,APP需要控制音量:播放/暂停;下一曲/下一曲;音量+/- 等
- 播放/暂停
/**
* 播放/暂停
*/
public void playMusic(Context context) {
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(downEvent);
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(upEvent);
}
- 下一曲
/**
* 下一曲
*/
public void nextMusic(Context context) {
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT);
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(downEvent);
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(upEvent);
}
- 上一曲
/**
* 上一曲
*/
public void lastMusic(Context context) {
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(downEvent);
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(upEvent);
}
- 音量 +
/**
* 音量 +
*/
public void setVolumeAdd(Context context) {
//音量 +
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FX_FOCUS_NAVIGATION_UP);
}
- 音量-
/**
* 音量 -
*/
public void setVolumeLess(Context context) {
//音量 -
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FX_FOCUS_NAVIGATION_UP);
}
- 设置当前音量
/**
* 设置当前音量
*
* @param context 上下文
* @param volume 当前音量百分比(0~1.0F)
*/
public void setVolume(Context context,float volume) {
//设置当前音量
AudioManager systemService = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int streamMaxVolume = systemService.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int data= Math.round(streamMaxVolume*volume);
systemService.setStreamVolume(AudioManager.STREAM_MUSIC, data, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
}
- 工具类(可以直接复制使用)
public class WatchAudioManagerUtil {
private static WatchAudioManagerUtil sInstance;
public static WatchAudioManagerUtil getInstance() {
if (sInstance == null) {
synchronized (WatchAudioManagerUtil.class) {
if (sInstance == null) {
sInstance = new WatchAudioManagerUtil();
}
}
}
return sInstance;
}
private WatchAudioManagerUtil() {
}
/**
* 播放/暂停
*/
public void playMusic(Context context) {
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(downEvent);
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(upEvent);
}
/**
* 下一曲
*/
public void nextMusic(Context context) {
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT);
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(downEvent);
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(upEvent);
}
/**
* 上一曲
*/
public void lastMusic(Context context) {
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(downEvent);
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(upEvent);
}
/**
* 音量 +
*/
public void setVolumeAdd(Context context) {
//音量 +
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FX_FOCUS_NAVIGATION_UP);
}
/**
* 音量 -
*/
public void setVolumeLess(Context context) {
//音量 -
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FX_FOCUS_NAVIGATION_UP);
}
/**
* 设置当前音量
*
* @param context 上下文
* @param volume 当前音量百分比(0~1.0F)
*/
public void setVolume(Context context,float volume) {
//设置当前音量
AudioManager systemService = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int streamMaxVolume = systemService.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int data= Math.round(streamMaxVolume*volume);
systemService.setStreamVolume(AudioManager.STREAM_MUSIC, data, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
}
}
评论区