前言
好久沒有發(fā)布文章了,整個(gè)5月底6月初的狀態(tài)都在準(zhǔn)備復(fù)習(xí)與面試的路上护奈,最終選擇了一家薪資待遇都不錯(cuò)的公司。之后會(huì)回憶一下面試相關(guān)的內(nèi)容做一下復(fù)盤。
問題發(fā)現(xiàn)
目前公司的登錄引導(dǎo)界面背景是無(wú)聲視頻乐尊,當(dāng)然采用VideoView來(lái)加載視頻。初到公司划址,熟悉業(yè)務(wù)扔嵌,偶然間發(fā)現(xiàn)第三方音樂播放器播放音樂在后臺(tái)限府,然后打開公司的App,啟動(dòng)引導(dǎo)頁(yè)痢缎,發(fā)現(xiàn)音樂被暫停了胁勺。其實(shí)想想很簡(jiǎn)單,就是VideoView的焦點(diǎn)被獲取了独旷,直接取消獲取音頻焦點(diǎn)就萬(wàn)事大吉了署穗。
解決問題
因?yàn)橹笆褂肰ideoView,一般都是有音樂的嵌洼,沒有注意到這個(gè)問題案疲。找了找發(fā)現(xiàn)并沒有能夠取消獲取音頻焦點(diǎn)的Api,然后就看了看源碼咱台。
Android O 8.0 之前
private void openVideo() {
if (mUri == null || mSurfaceHolder == null) {
// not ready for playback just yet, will try again later
return;
}
// we shouldn't clear the target state, because somebody might have
// called start() previously
release(false);
// 1
AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
//... 省略部分代碼
}
1處代碼可見络拌,在我們setVideoURI的時(shí)候,會(huì)去調(diào)用openVideo()的方法回溺,該方法在8.0之前會(huì)請(qǐng)求音頻焦點(diǎn)春贸,并且默認(rèn)就是申請(qǐng), 所以會(huì)導(dǎo)致其他正在播放的音頻停止。
Android 8.0 之后
/**
* Sets which type of audio focus will be requested during the playback, or configures playback
* to not request audio focus. Valid values for focus requests are
* {@link AudioManager#AUDIOFOCUS_GAIN}, {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT},
* {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK}, and
* {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE}. Or use
* {@link AudioManager#AUDIOFOCUS_NONE} to express that audio focus should not be
* requested when playback starts. You can for instance use this when playing a silent animation
* through this class, and you don't want to affect other audio applications playing in the
* background.
* @param focusGain the type of audio focus gain that will be requested, or
* {@link AudioManager#AUDIOFOCUS_NONE} to disable the use audio focus during playback.
*/
public void setAudioFocusRequest(int focusGain) {
if (focusGain != AudioManager.AUDIOFOCUS_NONE
&& focusGain != AudioManager.AUDIOFOCUS_GAIN
&& focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT
&& focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
&& focusGain != AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE) {
throw new IllegalArgumentException("Illegal audio focus type " + focusGain);
}
mAudioFocusType = focusGain;
}
/**
* Sets the {@link AudioAttributes} to be used during the playback of the video.
* @param attributes non-null <code>AudioAttributes</code>.
*/
public void setAudioAttributes(@NonNull AudioAttributes attributes) {
if (attributes == null) {
throw new IllegalArgumentException("Illegal null AudioAttributes");
}
mAudioAttributes = attributes;
}
private void openVideo() {
if (mUri == null || mSurfaceHolder == null) {
// not ready for playback just yet, will try again later
return;
}
// we shouldn't clear the target state, because somebody might have
// called start() previously
release(false);
if (mAudioFocusType != AudioManager.AUDIOFOCUS_NONE) {
// TODO this should have a focus listener
mAudioManager.requestAudioFocus(null, mAudioAttributes, mAudioFocusType, 0 /*flags*/);
}
}
在8.0之后提供了setAudioFocusRequest()的方法遗遵,也支持在xml布局中設(shè)置萍恕。但是8.0之前怎么辦呢?
解決方案就是因?yàn)闃I(yè)務(wù)需要车要,所以就可以復(fù)制一份VideoView源碼允粤,并且注釋requestAudioFocus()的方法。
總結(jié)
問題很簡(jiǎn)單翼岁,不過其實(shí)文章目的是介紹一種方式方法类垫,可以看Google工程師處理問題的解決方案,以及一些在ASOP中是怎樣fix bug的琅坡。
- 首先 https://cs.android.com/ 這個(gè)是我用來(lái)看ASOP的網(wǎng)站悉患。
2.選擇對(duì)應(yīng)的版本,查看git提交記錄榆俺。
因此這個(gè)就是找到本期問題的方式方法售躁,特此記錄下。