侧边栏壁纸
博主头像
X博主等级

一个移动端码农

  • 累计撰写 30 篇文章
  • 累计创建 40 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

android 音乐控制工具类

X
X
2022-10-31 / 0 评论 / 0 点赞 / 551 阅读 / 623 字

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);
    }
}

0

评论区