当前位置:操作系统 > 安卓/Android >>

Android歌词秀设计思路(4)通用的音乐播放服务(下)

这篇文章中我们将要说明在MediaPlayerService中用到的几个辅助功能。
1.AudioFocus相关处理
2.监视来电状态
3.监视耳机插头拔出
4.监视线控器按钮
5.Notification表示
AudioFocus相关处理
AudioFocus相关的处理已经被封装在AudioFocusHelper类中。这个类的直接目的虽然是为MediaPlayerService服务的,但是同时又独立与MediaPlayerService,可以独立使用。
功能
1.根据从AudioManager接收到的AudioFocus变化通知,管理内部的Focus状态。
2.结合内部状态和将通知转发给真正需要管理AudioFocus的类(在这里是MediaPlayerService类)
3.提供请求和释放AudioFocus的方法。
4.处理版本问题(AudioFocus只在Android2.2及以后的版本中可用)。
类图

我们在类图中
用蓝线标出了AudioFocus变化时通知的渠道(不是很严格)。当AudioManager发生AudioFocus的变化时,就会通知OnAudioFocusChangeListener,而这时的OnAudioFocusChangeListener实际上是由AudioFocusHelper提供的具象类的实例,在这个具象类中将通知处理后,又通知给作为MusicFocusable的具象类的MediaPlayerService。
用红线标出的AudioFocus请求和放弃的渠道:MediaPlayerServcie->AudioFocusHelper->AudioManager
以下AudioFocusHelper的源代码。
package LyricPlayer.xwg; 
 
import android.content.Context; 
import android.media.AudioManager; 
 
public class AudioFocusHelper { 
    AudioManager mAM; 
    MusicFocusable mFocusable; 
     
    // do we have audio focus? 
    public static final int NoFocusNoDuck = 0;    // we don't have audio focus, and can't duck 
    public static final int NoFocusCanDuck = 1;   // we don't have focus, but can play at a low volume ("ducking") 
    public static final int Focused = 2;           // we have full audio focus 
     
    private int mAudioFocus = NoFocusNoDuck; 
    private AudioManager.OnAudioFocusChangeListener mListener = null; 
         
    public AudioFocusHelper(Context ctx, MusicFocusable focusable) { 
        if (android.os.Build.VERSION.SDK_INT >= 8){ 
            mAM = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE); 
            mListener = new AudioManager.OnAudioFocusChangeListener(){ 
                /**  
                 * Called by AudioManager on audio focus changes. We implement this by calling our 
                 * MusicFocusable appropriately to relay the message. 
                 */
                @Override
                public void onAudioFocusChange(int focusChange) { 
                    if (mFocusable == null) return; 
                    switch (focusChange) { 
                        case AudioManager.AUDIOFOCUS_GAIN: 
                            mAudioFocus = Focused; 
                            mFocusable.onGainedAudioFocus(); 
                            break; 
                        case AudioManager.AUDIOFOCUS_LOSS: 
                        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: 
                             mAudioFocus = NoFocusNoDuck; 
                            mFocusable.onLostAudioFocus(); 
                            break; 
                        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: 
                             mAudioFocus = NoFocusCanDuck; 
                            mFocusable.onLostAudioFocus(); 
                            break; 
                         default: 
                    } 
                } 
                 
            }; 
            mFocusable = focusable; 
        }else{ 
             mAudioFocus = Focused; // no focus feature, so we always "have" audio focus 
        } 
    } 
 
    /** Requests audio focus. Returns whether request was successful or not. */
    public boolean requestFocus() { 
        return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == 
       

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,