AudioManager记录

AudioManager相关知识

一、介绍:系统服务的一种,主要用于获取特定音频的焦点,对音频进行设置
二、使用场景示例:
  1. 接听电话时,可以短暂获取焦点,使其他音频暂时停止播放。
  2. 拨打电话时可以获取焦点,拨打结束释放焦点。
  3. 录制语音、发送语音时获取焦点,使其他音频暂停,录制结束或发送结束后释放焦点,其他音频继续播放。
三、获取AudioManager
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
四、AudioManager常用方法简介
方法功能
int requestAudioFocus(AudioManager.OnAudioFocusChangeListener listener,int streamType,int durationHint)请求音频的焦点
void adjustStreamVolume(int streamType,int direction,int flags)调整手机指定类型的声音大小
void setMicrophoneMute(boolean on)设置是否让麦克风静音,true为静音
void setMode(int mode)设置声音模式
void setRingerMode(int ringerMode)设置手机电话铃声的模式
void setSpeakerphoneOn(boolean on)设置扬声器打开或关闭,免提效果,true为打开
void setStreamMute(int streamType,boolean state)将手机的指定类型的声音调整为静音
void setStremVolume(int streamTypemboolean state)设置手机的指定类型的音量值
五、参数及类型
  1. 申请焦点:int requestAudioFocus(AudioManager.OnAudioFocusChangeListener listener,int streamType,int durationHint)
    • listener :音频状态改变监听

    • streamType:音频类型。值为

      • AudioManager.STREAM_ALARM:手机闹钟
      • AudioManager.STREAM_RING:电话铃声
      • AudioManager.STREAM_MUSIC:手机音乐
      • AudioManager.STREAM_SYSTEM:手机系统
      • AudioManager.STREAM_DTMF:音调
      • AudioManger.STREAM_NOTIFICATION:系统提示
      • AudioManager.STREAM_VOICE_CALL:语音电话
    • durationHint:音频焦点状态
      1.1 获得焦点

      • AudioManager.AUDIOFOCUS_GAIN:应用获得AudioFocus;执行操作,播放或继续播放;失去焦点后会停止播放并释放资源。
      • AUDIOFOCUS_GAIN_TRANSIENT:暂时获取焦点,用于短暂的音频;失去焦点后只会暂停播放。
      • AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:应用与其他应用共用焦点,但播放时其他应,用会降低音量;失去焦点后会继续播放,但音量降低。

      1.2 失去焦点

      • AudioManager.AUDIOFOCUS_LOSS:应用失去了AudioFocus;执行操作:暂停播放。建议这时释放Media资源
      • AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:暂时时去AudioFocus,并会很快获得焦点 ,需要暂停播放,但可以不释放资源
      • FOCUS_LOSS_TRANSIENT_CAN_DUCK:暂时失去AudioFocus,但可以继续播放,但音量需要降低。
      • 返回值:
        AUDIOFOCUS_REQUEST_GRANTED:申请成功;
        AUDIOFOCUS_REQUEST_FAILED:申请失败。
  2. 放弃焦点
    abandonAudioFocus(AudioManager.OnAudioFocusChangeListener listener)
六、音频使用思路
  1. MyApp申请焦点进行音频播放 -> MyAPP播放中 -> 其他 APP申请焦点播放 -> MyApp失去焦点导致暂停播放 -> 其他APP播放
  2. 其他APP播放结束释放焦点 -> MyApp重新获得音频焦点,继续播放
分析蓝牙打印”05-22 20:55:15.340 3861 3861 W ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@1702c5f 05-22 20:55:15.405 3861 3861 W ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1005 android.content.ContextWrapper.sendBroadcast:444 com.zhiying.bluetoothmodelservice.MainActivity.sendBroadcast:643 com.zhiying.bluetoothmodelservice.MainActivity.onCreate:84 android.app.Activity.performCreate:7136 05-22 20:55:15.409 1936 5786 E ActivityManager: Sending non-protected broadcast android.newlink.exit.bluetoothSpeaker from system 3861:com.zhiying.bluetoothmodelservice/1000 pkg com.zhiying.bluetoothmodelservice 05-22 20:55:15.419 1936 5786 E ActivityManager: Sending non-protected broadcast android.newlink.exit.bluetoothSpeaker from system 3861:com.zhiying.bluetoothmodelservice/1000 pkg com.zhiying.bluetoothmodelservice 05-22 20:55:15.491 1797 1797 I MediaPlayerFactory: [getNameByPid:285] pid(3861), cmdline task_name(com.zhiying.bluetoothmodelservice). 05-22 20:55:15.561 3861 5918 W MediaPlayerNative: info/warning (710, 20) 05-22 20:55:15.562 3861 5918 W MediaPlayerNative: info/warning (710, 40) 05-22 20:55:15.610 3861 5918 W MediaPlayerNative: info/warning (710, 80) 05-22 20:55:15.628 3861 5918 W MediaPlayerNative: info/warning (710, 90) 05-22 20:55:15.628 3861 3861 I bt.sink.btconAc: true-------service-------- 05-22 20:55:15.628 3861 5918 W MediaPlayerNative: info/warning (710, 100) 05-22 20:55:15.629 3861 3861 W ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.startService:1531 android.content.ContextWrapper.startService:664 com.zhiying.bluetoothmodelservice.MainActivity.setBluetoothServiceStatus:354 com.zhiying.bluetoothmodelservice.MainActivity.initData:187 com.zhiying.bluetoothmodelservice.MainActivity.onCreate:89 05-22 20:55:15.649 3861 3861 I UartUtils: setState: true 05-22 20:55:15.649 3861 3861 I HiMW_TVClient: [invoke:53] =============invoke cmd = 0xf10a=======begin============= 05-22 20:55:15.653 3861 3861 I HiMW_TVClient: [invoke:65] =============invoke cmd = 0xf10a=======end=============== 05-22 20:55:15.654 3861 3861 W AudioManager: Use of stream types is deprecated for operations other than volume control 05-22 20:55:15.654 3861 3861 W AudioManager: See the documentation of requestAudioFocus() for what to use instead with android.media.AudioAttributes to qualify your playback use case 05-22 20:55:15.655 1936 5786 I MediaFocusControl: requestAudioFocus() from uid/pid 1000/3861 clientId=android.media.AudioManager@3140a29 callingPack=com.zhiying.bluetoothmodelservice req=2 flags=0x0 sdk=29 05-22 20:55:15.658 3861 3861 D bt.sink.btconAc: onResume: 05-22 20:55:15.658 3861 3861 D bt.sink.btconAc: getBreathingScreenDatas: Could not find remote control icon properties 05-22 20:55:15.702 3861 3861 I bt.sink.btsevice: blueToothChange:关闭 05-22 20:55:15.706 3861 3861 D BluetoothAdapter: enable(): BT already enabled! 05-22 20:55:15.738 3861 3894 I ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0 05-22 20:55:15.738 3861 3894 I ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retriev
05-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值