[How-To] Hook up device buttons on Playbook

Playbook has 4 buttons (only 4). They are “Power”, “Volume Up”, “Play / Pause” and “Volume Down”. If you are working on creating some Flash application for Playbook, you might want to consider react the button press on the device. Especially for a media player app, hook up the volume buttons and play / pause button are necessary. I will give you some example about how to take care of the button press on the Playbook.

Power Button:
When you press the power button, the device will switch to “Sleep Mode”, press again will wake it up. You application will be set “deactivate” & “activate” for those cases. So what you need to do is listen to Event.ACTIVATE and Event.DEACTIVATE just like this:

 NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, onAppActivate);  

NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, onAppDeactivate);

You should also know that the events will also be fired if the app has been switch to background (swipe up from the bottom of the screen and open another app).


Volume Up / Down & Mute / UnMute
Vomue Up / Down buttons simply do what they say. Long press the Play / Pause button causes Mute and UnMute. For accessing the volume information, you need to get an instance of Playbook’s AudioManager. And listen to the AudioManagerEvent.OUTPUT_LEVEL_CHANGED and AudioManagerEvent.OUTPUT_MUTE_CHANGED events. Just like this:

 // get audio manager instance  

audioMgr = AudioManager.audioManager;
// hook up volume up / down
audioMgr.addEventListener(AudioManagerEvent.OUTPUT_LEVEL_CHANGED, onDeviceVolumeChange);
// hood up mute / unmute (by long press the play/pause button)
audioMgr.addEventListener(AudioManagerEvent.OUTPUT_MUTE_CHANGED, onDeviceMuteChange);

Play / Pause
Play / Pause button belongs to Playbook’s MediaServiceConnection. A little more codes needs to be done for hooking up the button:

 mediaServiceConnection = new MediaServiceConnection;  

mediaServiceConnection.addEventListener(MediaServiceConnectionEvent.CONNECT, onMSConnect);
mediaServiceConnection.addEventListener(MediaServiceRequestEvent.TRACK_PAUSE, onMSTrackPause);
mediaServiceConnection.connect();

private function onMSConnect(event:MediaServiceConnectionEvent):void
{
mediaServiceConnection.requestAudioService();
}

private function onMSTrackPause(event:MediaServiceRequestEvent):void
{
// this is the place you get the button event, each press on the Play / Pause will fire this function
// there is no need to look the event detail b/c it's useless

doPlayPause();
}

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s