新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > android 音頻處理mediaplay介紹

android 音頻處理mediaplay介紹

作者: 時間:2016-09-12 來源:網(wǎng)絡(luò) 收藏

處理音頻焦點(diǎn)

本文引用地址:http://m.butianyuan.cn/article/201609/305045.htm

盡管某個時刻只有一個activity可以運(yùn)行,Android卻是一個多任務(wù)環(huán)境.這對使用音頻的應(yīng)用帶來了特殊的挑戰(zhàn),因為只有一個音頻輸出而可能多個媒體都想用它.在Android2.2之前,沒有內(nèi)建的機(jī)制來處理這個問題,所以可能在某些情況下導(dǎo)致壞的用戶體驗.例如,當(dāng)一個用戶正在聽音樂而另一個應(yīng)用需要通知用戶一些重要的事情時,用戶可能由于音樂聲音大而不能聽的通知.從Android2.2開始,平臺為應(yīng)用提供了一個協(xié)商它們?nèi)绾问褂迷O(shè)備音頻輸出的途徑,這個機(jī)制叫做音頻焦點(diǎn).

當(dāng)你的應(yīng)用需要輸出像樂音和通知之類的音頻時,你應(yīng)該總是請求音頻焦點(diǎn).一旦應(yīng)用具有了焦點(diǎn),它就可以自由的使用音頻輸出.但它總是應(yīng)該監(jiān)聽焦點(diǎn)的變化.如果被通知丟失焦點(diǎn),它應(yīng)該立即殺死聲音或降低到靜音水平(有一個標(biāo)志表明應(yīng)選擇哪一個)并且僅當(dāng)重新獲得焦點(diǎn)后才恢復(fù)大聲播放.

將來的音頻焦點(diǎn)是合作的.所以,應(yīng)用被希望(并被強(qiáng)列鼓勵)遵守音頻焦點(diǎn)的方針,但是卻不是被系統(tǒng)強(qiáng)制的.如果一個應(yīng)用在丟失音頻焦點(diǎn)后依然想大聲播放音樂,系統(tǒng)不會去阻止它.然而用戶卻體驗很壞并且很想把這鳥應(yīng)用卸載.

要請求音頻焦點(diǎn),你必須從AudioManager調(diào)用requestAudioFocus(),如下所示:

[java]

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,

AudioManager.AUDIOFOCUS_GAIN);

if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {

// 不能獲得音頻焦點(diǎn)

}

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,

AudioManager.AUDIOFOCUS_GAIN);

if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {

// 不能獲得音頻焦點(diǎn)

}

requestAudioFocus()的第一個參數(shù)是一個AudioManager.OnAudioFocusChangeListener,它的onAudioFocusChange()方法在音頻焦點(diǎn)發(fā)改變時被調(diào)用.因此,你也應(yīng)該在你的service和activity上實(shí)現(xiàn)此接口.例如:

[java]

class MyService extends Service

implements AudioManager.OnAudioFocusChangeListener {

// ....

public void onAudioFocusChange(int focusChange) {

// Do something based on focus change...

}

}

class MyService extends Service

implements AudioManager.OnAudioFocusChangeListener {

// ....

public void onAudioFocusChange(int focusChange) {

// Do something based on focus change...

}

}

參數(shù)focusChange告訴你音頻焦點(diǎn)如何發(fā)生了變化,它可以是以上幾種值(它們都是定義在AudioManager中的常量):

AUDIOFOCUS_GAIN:你已獲得了音頻焦點(diǎn).

AUDIOFOCUS_LOSS:你已經(jīng)丟失了音頻焦點(diǎn)比較長的時間了.你必須停止所有的音頻播放.因為預(yù)料到你可能很長時間也不能再獲音頻焦點(diǎn),所以這里是清理你的資源的好地方.比如,你必須釋放MediaPlayer.

AUDIOFOCUS_LOSS_TRANSIENT:你臨時性的丟掉了音頻焦點(diǎn),很快就會重新獲得.你必須停止所有的音頻播放,但是可以保留你的資源,因為你可能很快就能重新獲得焦點(diǎn).

AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:你臨時性的丟掉了音頻焦點(diǎn),但是你被允許繼續(xù)以低音量播放,而不是完全停止.

下面是一個例子:

[java]

public void onAudioFocusChange(int focusChange) {

switch (focusChange) {

case AudioManager.AUDIOFOCUS_GAIN:

// resume playback

if (mMediaPlayer == null) initMediaPlayer();

else if (!mMediaPlayer.isPlaying()) mMediaPlayer.start();

mMediaPlayer.setVolume(1.0f, 1.0f);

break;

case AudioManager.AUDIOFOCUS_LOSS:

// Lost focus for an unbounded amount of time: stop playback and release media player

if (mMediaPlayer.isPlaying()) mMediaPlayer.stop();

mMediaPlayer.release();

mMediaPlayer = null;

break;

case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:

// Lost focus for a short time, but we have to stop

// playback. We don't release the media player because playback

// is likely to resume

if (mMediaPlayer.isPlaying()) mMediaPlayer.pause();

break;

case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:

// Lost focus for a short time, but it's ok to keep playing

// at an attenuated level

if (mMediaPlayer.isPlaying()) mMediaPlayer.setVolume(0.1f, 0.1f);

break;

}

}

public void onAudioFocusChange(int focusChange) {

switch (focusChange) {

case AudioManager.AUDIOFOCUS_GAIN:

// resume playback

if (mMediaPlayer == null) initMediaPlayer();

else if (!mMediaPlayer.isPlaying()) mMediaPlayer.start();

mMediaPlayer.setVolume(1.0f, 1.0f);

break;

case AudioManager.AUDIOFOCUS_LOSS:

// Lost focus for an unbounded amount of time: stop playback and release media player

if (mMediaPlayer.isPlaying()) mMediaPlayer.stop();

mMediaPlayer.release();

mMediaPlayer = null;

break;

case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:

// Lost focus for a short time, but we have to stop

// playback. We don't release the media player because playback


上一頁 1 2 下一頁

關(guān)鍵詞:

評論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉