新聞中心

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

android 音頻處理mediaplay介紹

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

// is likely to resume

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

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;

}

}

記住音頻焦點(diǎn)API僅在APIlevel 8 (Android2.2)及更高版本上可以,所以如果你想支持更早的Android版本,你必須在可能時(shí)采取兼容性的策略使用特性,如果不可能,you should adopt a backward compatibility strategy that allows you touse this feature if available, and fall back seamlessly if not.

你可以用反射的方式調(diào)用音頻焦點(diǎn)方法或自己在一個(gè)單獨(dú)的類(叫做AudioFocusHelper)中實(shí)現(xiàn)所有的音頻焦點(diǎn)功能來(lái)達(dá)到向前兼容.下面是一個(gè)這樣的類:

[java]

public class AudioFocusHelper implements AudioManager.OnAudioFocusChangeListener {

AudioManager mAudioManager;

// 這里是其它的字段,你可能要保存一個(gè)接口的引用,這個(gè)接口

// 被用于與你的service通訊以報(bào)告焦點(diǎn)的變化.

public AudioFocusHelper(Context ctx, ) {

mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

// ...

}

public boolean requestFocus() {

return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==

mAudioManager.requestAudioFocus(mContext, AudioManager.STREAM_MUSIC,

AudioManager.AUDIOFOCUS_GAIN);

}

public boolean abandonFocus() {

return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==

mAudioManager.abandonAudioFocus(this);

}

@Override

public void onAudioFocusChange(int focusChange) {

// 讓你的service知道焦點(diǎn)變化了

}

}

你可以僅在檢測(cè)到系統(tǒng)運(yùn)行的是API level 8 或更早的版本時(shí)才創(chuàng)建AudioFocusHelper類的實(shí)例.例如:

if (android.os.Build.VERSION.SDK_INT >= 8) {

mAudioFocusHelper = new AudioFocusHelper(getApplicationContext(), this);

} else {

mAudioFocusHelper = null;

}

public class AudioFocusHelper implements AudioManager.OnAudioFocusChangeListener {

AudioManager mAudioManager;

// 這里是其它的字段,你可能要保存一個(gè)接口的引用,這個(gè)接口

// 被用于與你的service通訊以報(bào)告焦點(diǎn)的變化.

public AudioFocusHelper(Context ctx, ) {

mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

// ...

}

public boolean requestFocus() {

return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==

mAudioManager.requestAudioFocus(mContext, AudioManager.STREAM_MUSIC,

AudioManager.AUDIOFOCUS_GAIN);

}

public boolean abandonFocus() {

return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==

mAudioManager.abandonAudioFocus(this);

}

@Override

public void onAudioFocusChange(int focusChange) {

// 讓你的service知道焦點(diǎn)變化了

}

}

你可以僅在檢測(cè)到系統(tǒng)運(yùn)行的是API level 8 或更早的版本時(shí)才創(chuàng)建AudioFocusHelper類的實(shí)例.例如:

if (android.os.Build.VERSION.SDK_INT >= 8) {

mAudioFocusHelper = new AudioFocusHelper(getApplicationContext(), this);

} else {

mAudioFocusHelper = null;

}

清理

前面提到過(guò),一個(gè)MediaPlayer對(duì)象可以消耗掉大量的系統(tǒng)資源,所以你應(yīng)該僅在需要它時(shí)保持它并在用完時(shí)立即釋放.明確的調(diào)用清理方法而不是依靠系統(tǒng)的垃圾收集機(jī)制是很重要的,因?yàn)樵诒皇占癕ediaPlayer可能會(huì)存在很長(zhǎng)時(shí)間,雖然此時(shí)它只是占用內(nèi)存而不影響其它的媒體相關(guān)的資源.所以,當(dāng)你使用一個(gè)service時(shí),你應(yīng)該總四重寫onDestroy()方法來(lái)保證釋放MediaPlayer:

[java]

public class MyService extends Service {

MediaPlayer mMediaPlayer;

// ...

@Override

public void onDestroy() {

if (mMediaPlayer != null) mMediaPlayer.release();

}

}

public class MyService extends Service {

MediaPlayer mMediaPlayer;

// ...

@Override

public void onDestroy() {

if (mMediaPlayer != null) mMediaPlayer.release();

}www.2cto.com

}

你也應(yīng)該尋找其它需要釋放你的MediaPlayer的時(shí)機(jī).例如,如果你預(yù)料到長(zhǎng)時(shí)間不能播放媒體(比如丟掉音頻焦點(diǎn)以后),你應(yīng)該明確地釋放你的MediaPlayer,然后在后面重新創(chuàng)建它.反過(guò)來(lái),如果你預(yù)測(cè)到只會(huì)短時(shí)間停止播放,你應(yīng)該保持你的MediaPlayer來(lái)避免過(guò)多的創(chuàng)建,而不是重新準(zhǔn)備它.


上一頁(yè) 1 2 下一頁(yè)

關(guān)鍵詞:

評(píng)論


相關(guān)推薦

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

關(guān)閉