昨天遇到一个bug,经过debug发现对audiofocus的理解还是不够深,bug是需要在camera preview mode能播放音乐,“咔”的时候停止,然后继续播放。
但是现象是“咔”以后就不播放了。重复看了下android的参数描述,发现AUDIOFOCUS_GAIN与AUDIOFOCUS_GAIN_TRANSIENT是有区别
在使用AudioFocus获取音频焦点时,这两个参数都可以获取焦点。但两个参数的解释却不一样
public static final int AUDIOFOCUS_GAIN
Used to indicate a gain of audio focus, or a request of audio focus, of unknown duration.获取后不release,需要手动release
public static final int AUDIOFOCUS_GAIN_TRANSIENT
Used to indicate a temporary gain or request of audio focus, anticipated to last a short amount of time. Examples of temporary changes are the playback of driving directions, or an event notification.临时获取,使用后release
所以拍照时候“咔”应该使用AUDIOFOCUS_GAIN_TRANSIENT