📜  Android中的AudioManager(带示例)(1)

📅  最后修改于: 2023-12-03 15:29:22.610000             🧑  作者: Mango

Android中的AudioManager(带示例)

Android的AudioManager是一个管理音频的类,可以控制音量,播放铃声,音乐等,还可以监听各种音频焦点变化。

常用方法
  • setMode(int mode): 设置音频模式,通常是普通模式(NORMAL)、铃声模式(RINGTONE)、通知模式(NOTIFICATION)等。例如:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_NORMAL);
  • setStreamVolume(int streamType, int index, int flags): 设置音量,第一个参数是音频类型,如STREAM_MUSIC(音乐)、STREAM_ALARM(闹钟)等,第二个参数是音量值,第三个参数是标志位,如FLAG_SHOW_UI(显示音量条)、FLAG_PLAY_SOUND(音量改变时播放声音)等。例如:
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 5, AudioManager.FLAG_PLAY_SOUND);
  • getStreamVolume(int streamType): 获取音量。例如:
int musicVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
  • preAudioFocusRequest(int focusGain, int usage, AudioAttributesCompat aa, IAudioFocusDispatcher afl, IBinder binder, int sdk, int flags, int[] grantResult, String clientId, IHwBinder hwtoken, long durationHintMs, float speed): 通过IManager服务优先申请音频焦点。例如:
audioManager.preAudioFocusRequest(AudioManager.AUDIOFOCUS_GAIN, AudioManager.USAGE_MEDIA, null,null,null,0,FLAG_NONE,
            new int[]{AudioManager.AUDIOFOCUS_REQUEST_GRANTED},null,null,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT,0.0f);
  • requestAudioFocus(OnAudioFocusChangeListener listener, int streamType, int durationHint): 申请音频焦点。例如:
AudioManager.OnAudioFocusChangeListener focusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
    public void onAudioFocusChange(int focusChange) {
        // 处理焦点变化
    }
};
int focusRequest = audioManager.requestAudioFocus(focusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
  • abandonAudioFocus(OnAudioFocusChangeListener listener): 释放音频焦点。例如:
audioManager.abandonAudioFocus(focusChangeListener);
监听音频焦点变化的方式
  • OnAudioFocusChangeListener: 定义一个监听器,当音频焦点发生变化时回调该接口的方法。
AudioManager.OnAudioFocusChangeListener focusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
    public void onAudioFocusChange(int focusChange) {
        // 处理焦点变化
    }
};
  • AudioFocusRequest: Android 8.0及以上版本引入了新的方式,使用AudioFocusRequest来申请音频焦点。例如:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
AudioFocusRequest focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
            .setAudioAttributes(new AudioAttributesCompat.Builder()
                    .setUsage(AudioAttributesCompat.USAGE_MEDIA)
                    .setContentType(AudioAttributesCompat.CONTENT_TYPE_MUSIC)
                    .build())
            .setAcceptsDelayedFocusGain(true)
            .setOnAudioFocusChangeListener(focusChangeListener)
            .build();
int result = audioManager.requestAudioFocus(focusRequest);
代码实现
  • 首先需要获取AudioManager实例。
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  • 设置音量。
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 5, AudioManager.FLAG_PLAY_SOUND);
  • 监听音频焦点变化。
AudioManager.OnAudioFocusChangeListener focusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
    public void onAudioFocusChange(int focusChange) {
        // 处理焦点变化
    }
};
int focusRequest = audioManager.requestAudioFocus(focusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
总结

Android的AudioManager类提供了强大的功能,可以方便地控制音频,处理音频焦点等问题。熟练掌握该类的使用,对音频相关功能的开发非常有帮助。