Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
New Developer
Posts: 63
Registered: ‎10-27-2010
My Device: 9900 and PlayBook
My Carrier: Rogers

How to Play Sound While PlayBook is in Standby Mode

I recently figured out how to continue to play sound while the PlayBook is in Standby mode and wanted to share, given a lack of docs on the subject.


The following code would work on its own and doesn't seem to affect active mode.  Apparently the alarm only occurs while the device is in standby.  As soon as the device goes into standby, any alarms that are set by your app kick in.


Running a complete frame cycle on an alert that occurs as often as possible (0ms) works really well (and presumably would work for any timers you have running). I haven't found any major drawbacks using 0ms for the duration of the alarm.I tried matching my frame rate (1000/30) but that wasn't fast enough.


Of course, you may also want to cancel the alarm when not playing any sound (see QNXSystem.system.cancelAlarm), as I'm sure that the frame cycles burn precious battery, even when the device is in standby. Not as much as when in active, but probably best to have the alarm running only when necessary.


Hope you find this useful.

 

QNXSystem.system.addEventListener(qnx.events.QNXSystemEvent.ALARM, function(e:Event):void {
	QNXSystem.system.runFrameCycle();
});
var alarmId:int = QNXSystem.system.setAlarm(0, true);

 

-----------------------------------------------------------------------------
Apps:Beats Brainwave Entrainment - Awesome Timer - StarSlide - Save Santa
@jamiebeach
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: How to Play Sound While PlayBook is in Standby Mode

[ Edited ]

Please don't anyone follow the above advice, however, as there are much better (and even documented) ways of doing what you need.

 

They published an article on Power Management back in March, and it covers the mentioned setAlarm() function as well as numerous related things.

 

If you merely want to play sound, you may need nothing more than to use <inactivePowerMode>throttled</inactivePowerMode> in your blackberry-tablet.xml file.  This will result in your app getting 4 frame per second (or maybe it's 5... can never remember) when it's inactive (whether the device is in standby or not).  This is generally enough to keep things going adequately for sound, even if you're using SampleDataEvent to generate the sound "raw".

 

(Edit: I said "April" at first but turns out that's only when I first saw it and posted a thread about it.)


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
New Developer
Posts: 63
Registered: ‎10-27-2010
My Device: 9900 and PlayBook
My Carrier: Rogers

Re: How to Play Sound While PlayBook is in Standby Mode

[ Edited ]

I'll have to give that a try then.

I had initially thought that 4Hz wasn't enough to play a 44100 samples/second rate, given that I can only spit out a max of 8192 samples per SampleDataEvent (which I'd presume would only get fired at 4Hz).  Plus that documentation says a maximum of 4Hz.

-----------------------------------------------------------------------------
Apps:Beats Brainwave Entrainment - Awesome Timer - StarSlide - Save Santa
@jamiebeach
Contributor
Posts: 11
Registered: ‎05-28-2011
My Device: playbook
My Carrier: n/a

Re: How to Play Sound While PlayBook is in Standby Mode

<inactivePowerMode>throttled</inactivePowerMode>

is more than enough , I keep playing sound of movies (MovieSrtplayer and MovieFlvplayer) an only sound (BelgiumRadio) in inactive mode and it works just fine Smiley Happy

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: How to Play Sound While PlayBook is in Standby Mode

My White Noise generator uses THROTTLED and works flawlessly :-), though I do have to say that in my DEACTIVATE handler I current set stage.frameRate to 5, rather than using the default 4.  I don't have a clear memory of whether I really need that, whether it's a holdover from before inactivePowerMode was supported, or what.  The rationale for the code is not documented :-(.  (Bad programmer, bad!)

 

My best theory is that in the simulator, with the default, I would sometimes get the odd hiccup so I set it to 5 to try to avoid that.  Another possibility is that they previously used 5 and as I had experience testing with that I kept the old value when I noticed they'd changed to 4... can't say for sure (most likely it was always 4).

 

Note that even 5Hz is below the 44100/8192 (5.4Hz) value, and maybe it should be 44100/4096 (10.8Hz) since you have to generate samples for both left and right channels.  It's clear that the sound subsystem must pregenerate several buffer's worth of data to stay ahead of things, or it would glitch very often even at 5Hz.

 

Try the default (i.e. just use THROTTLED) and don't complicate things by changing the frameRate unless you actually experience problems.  Some day I'll retest thoroughly with mine and, I hope, can just ditch that frame rate override code.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
New Developer
Posts: 63
Registered: ‎10-27-2010
My Device: 9900 and PlayBook
My Carrier: Rogers

Re: How to Play Sound While PlayBook is in Standby Mode

@peter9477 - thanks for the great explanations.  I must say that I've relied on your commentary in the forums on numerous ocassions.  Great stuff.  Keep up the great work!

I'll give the throttled mode (with default framerate) a go later tonight and see what happens.

-----------------------------------------------------------------------------
Apps:Beats Brainwave Entrainment - Awesome Timer - StarSlide - Save Santa
@jamiebeach
Highlighted
New Developer
Posts: 63
Registered: ‎10-27-2010
My Device: 9900 and PlayBook
My Carrier: Rogers

Re: How to Play Sound While PlayBook is in Standby Mode

Just to follow up, I simply set the inactivePowerMode to THROTTLED in the main constructor and it worked.  Didn't modify the frames per second or anything.  Admittedly, I didn't believe it would given the multiple audio channels that my app uses - all of which generate raw sample data (some cycles far less than the max samples).  Thought for sure I'd hear some clicks, but it's nice and smooth.

 

So I'm a happy camper now (and my users will be as well Smiley Wink )

-----------------------------------------------------------------------------
Apps:Beats Brainwave Entrainment - Awesome Timer - StarSlide - Save Santa
@jamiebeach