频道栏目
首页 > 资讯 > Android > 正文

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

11-09-13        来源:[db:作者]  
收藏   我要投稿

这篇文章中我们将要说明在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 == 
            mAM.requestAudioFocus(mListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); 
    } 
 
    /** Abandons audio focus. Returns whether request was successful or not. */
    public boolean abandonFocus() { 
        return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAM.abandonAudioFocus(mListener); 
    } 
 
    public void giveUpAudioFocus() { 
        if (mAudioFocus == Focused  
                && android.os.Build.VERSION.SDK_INT >= 8
                && abandonFocus()) 
                mAudioFocus = NoFocusNoDuck; 
    } 
     
    public void tryToGetAudioFocus() { 
        if (mAudioFocus != Focused  
                && android.os.Build.VERSION.SDK_INT >= 8
                && requestFocus()) 
            mAudioFocus = Focused; 
    } 
     
    int getAudioFocus(){ 
        return mAudioFocus; 
    } 

 监视来电状态
AudioFocus是Android2.2以后才有的功能,对于比2.2低得版本,用的是另一种方法,就是监听电话的状态。最起码在电话打进来是能够暂停音乐的播放。
实现这一功能的第一步是在AndroidManifest.xml中声明用于接收PHONE_STATE通知的receiver
<receiver android:name=".PhoneStateReceiver">  
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE"/>
    </intent-filter>  
</receiver> 
第二步是定义一个对应的PhoneStateReceiver,代码如下
package LyricPlayer.xwg; 
 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.TelephonyManager; 
 
public class PhoneStateReceiver extends BroadcastReceiver { 
    @Override
    public void onReceive(Context context, Intent intent) { 
        //if android.os.Build.VERSION.SDK_INT >= 8 we use audio focus. 
        if (android.os.Build.VERSION.SDK_INT < 8){ 
            TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
            if(tm.getCallState() != TelephonyManager.CALL_STATE_IDLE){ 
                context.startService(new Intent(MediaPlayerService.ACTION_PAUSE)); 
            } 
        } 
    } 
}
这就够了。
监视耳机插头拔出
如果在音乐播放过程中拔出耳机,音乐就会通过扬声器播放出来。为了避免这种尴尬局面,我们会监视耳机拔出状态,并在耳机拔出时暂停播放。
首先是在AndroidManifest.xml中声明用于接收AUDIO_BECOMING_NOISY通知的receiver
<receiver android:name=".MusicIntentReceiver"> 
    <intent-filter> 
        <action android:name="android.media.AUDIO_BECOMING_NOISY" /> 
    </intent-filter> 
</receiver>
然后就是定义用于处理通知的receiver,类名要和AndroidManifest.xml中声明的一样。
package LyricPlayer.xwg; 
 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
 
public class MusicIntentReceiver extends BroadcastReceiver { 
    @Override
    public void onReceive(Context ctx, Intent intent) { 
        if (intent.getAction().equals(android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) { 
            ctx.startService(new Intent(LyricPlayerService.ACTION_PAUSE)); 
        } 
    } 
}
MEDIA_BUTTON处理
在讨论处理方法之前,必须先明确:那些键属于MEDIA_BUTTON?根据我的试验,MEDIA_BUTTON好像就是线控上面的上个按钮。网上也有用同样的方法取得音量键动作的内容,但是我没有试出来。
继续我们的话题,为了检测MEDIA_BUTTON需要一些准备工作。
首先是在AndroidManifest.xml中声明用于接收MEDIA_BUTTON通知的receiver
<receiver android:name="MediaButtonReceiver">
    <intent-filter>         
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>
当然需要定义真正的receiver,名字要和AndroidManifest.xml中的一样。
package LyricPlayer.xwg; 
 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import android.view.KeyEvent; 
 
public class MediaButtonReceiver extends BroadcastReceiver { 
    private static final String TAG = new String("LyricVolumeKeyReceiver"); 
    @Override
    public void onReceive(Context context, Intent intent) { 
        //MusicPlaybackService service = (MusicPlaybackService)context; 
         if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) { 
             KeyEvent key = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); 
             if(key.getAction() == KeyEvent.ACTION_DOWN){ 
                 Log.i(TAG, "OnReceive, getKeyCode = " + key.getKeyCode()); 
                 switch(key.getKeyCode()){ 
                 case KeyEvent.KEYCODE_HEADSETHOOK : 
                     context.startService(new Intent(MediaPlayerService.ACTION_PLAY_PAUSE)); 
                     break; 
                 case KeyEvent.KEYCODE_MEDIA_PREVIOUS: 
                     context.startService(new Intent(MediaPlayerService.ACTION_PREVIOUS)); 
                     break; 
                 case KeyEvent.KEYCODE_MEDIA_NEXT: 
                     context.startService(new Intent(MediaPlayerService.ACTION_NEXT)); 
                     break; 
                 } 
             } 
        } 
    } 
}
比较特别的是中间的键的键值不是KEYCODE_PLAY_PAUSE而是KEYCODE_HEADSETHOOK。想想也是,接电话也用这个键。
准备工作的最后一步就是要把通过MediaButtonReceiver来接受MEDIA_BUTTON这件事报告给AudioMenager,由于这也是Android2.2及以后版本才有的功能,也需要做版本判断。
if (android.os.Build.VERSION.SDK_INT >= 8){ 
            mReceiverName = new ComponentName(getPackageName(),MediaButtonReceiver.class.getName()); 
            mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
            mAudioManager.registerMediaButtonEventReceiver(mReceiverName); 
        }
当然在结束的时候我们也会保持取消登录的良好习惯。
if(mAudioManager != null && mReceiverName != null){ 
            mAudioManager.unregisterMediaButtonEventReceiver(mReceiverName); 
        }
Notification表示
Notification表示首先取得NotificationManager
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
在需要表示的时候调用showNotification()方法。和showNotification()方法有关的代码:
public interface NotificationProvider{ 
        public Notification createNotification(Context context); 
    } 
     
    NotificationProvider mNotificationProvider = null; 
     
    public void setNotificationProvider(NotificationProvider provider){ 
        mNotificationProvider = provider; 
    } 
     
    /** * Show a notification while this service is running.     */
    private void showNotification() { 
        if(mNotificationProvider != null){ 
        // Send the notification. 
            mNotificationManager.notify(NOTIFICATION, mNotificationProvider.createNotification(this));     
        } 
    }
已经用了N次的办法了。不用再解释了吧。当然,看看实现侧的做法还有必要的。
mProxy.setNotificationProvider(new MediaPlayerService.NotificationProvider(){ 
            @Override
            public Notification createNotification(Context context) { 
                Notification notification = new Notification(R.drawable.button_blue_play, mProxy.getTitle(), System.currentTimeMillis()); 
                // The PendingIntent to launch our activity if the user selects this notification 
                PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, LyricMain.class), 0);  
                // Set the info for the views that show in the notification panel. 
                notification.setLatestEventInfo(context, getText(R.string.media_player_label), mProxy.getTitle(), contentIntent); 
                return notification; 
            } 
        });
代码本身没有什么,都是程式化的东西。
最后就是在不再需要表示Notification的时候,执行以下代码
mNotificationManager.cancel(NOTIFICATION);
完整的代码请参照以下博文的附件。
软件功能说明:原创:Android应用开发-Andorid歌词秀,含源码
工程,源码下载:Android歌词秀源码,工程文件2011/9/11版
 
作者“来自大连”

相关TAG标签
上一篇:Android歌词秀设计思路(3)通用的音乐播放服务(上)
下一篇: Android歌词秀设计思路(5)歌词播放服务
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站