AUDIOFOCUS_GAIN与AUDIOFOCUS_GAIN_TRANSIENT的区别

昨天遇到一个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


其实在musicplayer里对音频焦点的设置是AUDIOFOCUS_LOSS_TRANSIENT,也就是说临时失去,所以会再咔以后继续播放

你可能感兴趣的:(android,音乐,audio,playback)