free web page counters

Windows Mobile Pocket PC Smartphone Programming

==>Click here for the SiteMap<==. Original contents with decent amount of source codes.

Thursday, May 11, 2006

Pocket PC Power Management Series 11: Play sound (for example, wav file) when a Pocket PC is suspended

====>SiteMap of this Blog<===

Pocket PC Power Management Series 11: Play sound (for example, wav file) when a Pocket PC is suspended

I am going to write a new series on Pocket PC Power Management topic: How to play sound when Pocket PC is sleeping or suspended. Let me use a simple API to achieve this purpose: PlaySound.

Why care?

If you read my previous series on the Power Management topic, you may ask why we care. If the Pocket PC sleeps, no thread can run, so PlaySound won't even be called. If the PPC is not sleeping, PlaySound is called, so why the hassle? The answer is: Yes, PlaySound will be called, but you may not hear the sound! So the title of this blog is sort of misleading. It should be changed to something like "How to make sure there is a sound when the device is suspended".

Let us say, you write the code to request the PPC to play a wav file, even when it is suspended. A good example is the Alarm program. It might need to play a warning or ringtone at 8PM. This piece of code can be executed at 8PM, guaranteed by Windows CE OS. However, you may be disappointed not hearing any sound. Below I will explain why and how to solve the problem.

Why no Sound?

Ultimately a peripheral device needs to emit the sound. This peripheral must be powered or put into certain state so as to vibrate to produce sound waves. A good chance is: When PlaySound() is called, the Pocket PC is in such a "System Power State" that the sound peripheral is not supposed to work; or in Microsoft terms, the sound peripheral is in such a "Device Power State" mapped from the current "System Power State", that the sound peripheral might have no power, have low power or in standby mode.

For our specific alarm example, the Pocket PC might be in a "System Power State" called "Unattended". The sound peripheral is called "wav1:", which is put into a "D4 Device Power State", mapped from "Unattended" state. "D4" means "No Power", so you won't be able to hear any sound.

To above explanation might be too abstract. If you really need to understand those terms, please read Microsoft Platform Builder documentation on "Power Management".

A Possible Solution

A possible solution to the problem is to change the mapping, so as to change the power state of "wav1:" device. The following code snippet is doing this job:

HRESULT ChangeWav1Mapping()
{
   LRESULT lr = E_FAIL;

   HKEY hKey = NULL;
   DWORD dwSubKeys = 0;
   DWORD dwMaxSubkeyLen = 0;
   TCHAR szSubkeyName[MAX_PATH];

   lr = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("\\System\\CurrentControlSet\\Control\\Power\\State"), 0, 0, &hKey);
   if (ERROR_SUCCESS != lr) goto FuncExit;

   lr = RegQueryInfoKey(hKey, NULL, 0, 0, &dwSubKeys, &dwMaxSubkeyLen, NULL, NULL, NULL, NULL, NULL, NULL);
   if (ERROR_SUCCESS != lr) goto FuncExit;

   for (int i=0; i<dwSubKeys; i++) {
      DWORD dwSubkeyNameLen = MAX_PATH-1;
      memset(szSubkeyName, 0, MAX_PATH*sizeof(TCHAR));
      lr = RegEnumKeyEx(hKey, i, szSubkeyName, &dwSubkeyNameLen, NULL, NULL, NULL, NULL);
      if (ERROR_SUCCESS != lr) continue;
      
      //ALERT(szSubkeyName);

      HKEY hSubkey = NULL;
      lr = RegOpenKeyEx(hKey, szSubkeyName, 0, 0, &hSubkey);
      if (ERROR_SUCCESS != lr) continue;

      // any wav1: ?
      DWORD dwValueType = 0;
      DWORD dwValueData = 0;
      DWORD dwValueDataLen = sizeof(DWORD);
      lr = RegQueryValueEx(hSubkey, TEXT("wav1:"), NULL, &dwValueType, (LPBYTE)&dwValueData, &dwValueDataLen);
      if (ERROR_SUCCESS == lr && 0 != dwValueData) {
         dwValueData = 0;
         RegSetValueEx(hSubkey, TEXT("wav1:"), NULL, REG_DWORD, (LPBYTE)&dwValueData, sizeof(DWORD));
      }
      RegCloseKey(hSubkey);
   }

   RegFlushKey(hKey);

FuncExit:
   if (hKey) {
      RegCloseKey(hKey);
   }

   return S_OK;
}

Notice this is a piece of bad code. It forces the "wav1:" device to be always powered, no matter what state the Pocket PC is in. Looks like that is the case for most Pocket PC 2003 devices, but probably not true for Pocket PC 5.0 device. So the above code effectively changes the sound behavior of PPC 5.0 device.

The recommended API to use to SetPowerRequirement and ReleasePowerRequirement. I will talk about them in a later post.

Wrap Up

Pocket PC sleeps. Read the following two posts for basis:

There are ways to force the device not to sleep, or request it to run a program at certain time or upon certain event:

The above techniques force the CPU to run, but not necessarily other peripheral devices. In this post we talked about "wav1:" and a possible hacky way to override the power mapping.

====>SiteMap of this Blog<===




[ [permalink] ]

9 Comments:

At February 21, 2007 11:45 PM, Anonymous Anonymous said...

Hi.
I saw your post about MAPI.i have a question.
When i try to find message store related to sms,i check the string of pr-display to be SMS,i find out that in some devices it is 'default'.
beside checking the string is there any way to find the msg store of SMS

thanks

 
At June 18, 2007 3:05 AM, Anonymous Anonymous said...

I'm looking for the best solution to my predicament;
I have a GPS Tracking app written in C# and when the device suspends the the GPS signal is not processed, I can prevent the device from suspending, but that leaves the backlight on, I can turn the screen off and leave the device in awake mode but I because my app runs in the background I want to be able to turn the screen off after a period of inactivity.
Please help ASAP

 
At October 21, 2007 7:37 AM, Blogger pocketpc said...

Very good!
My site http://www.pocketfree.blogspot.com/ hosts the latest and most interesting PocketPC Freeware and News. You're welcome to post links of such programs to be published on the site on pocketfree@gmail.com

 
At April 24, 2009 9:43 PM, Anonymous Anonymous said...

I always heard something from my neighbor that he sometimes goes to the internet bar to play the game which will use him some Perfect World Gold, he usually can win a lot of Buy Perfect World Gold, then he let his friends all have some Perfect World Silver, his friends thank him very much for introducing them the Perfect World money, they usually cheap Perfect World Gold together. It is the pw gold always make them happy.
I am so happy to get some Pirates of the Burning Sea Gold and the potbs gold is given by my close friend who tells me that the potbs Doubloon is the basis to enter into the game. Therefore, I should potbs money with the spare money and I gain some buy potbs Doubloon from other players.

 
At April 25, 2009 2:10 AM, Anonymous Anonymous said...

Have you heared about 9Dragons which you need use Anarchy credits to play, and you can also borrow Anarchy Online credits from other players? But you can buy AO credits, or you will lose the choice if you do not have Anarchy online gold. If you get Anarchy gold, you can continue this game.
Have you heared about 9Dragons which you need use 9Dragons gold to play, and you can also borrow 9 Dragons gold from other players? But you can buy 9 Dragons gold, or you will lose the choice if you do not have cheap 9Dragons gold. If you get 9Dragons money, you can continue this game.

 
At April 25, 2009 2:57 AM, Anonymous Anonymous said...

I like hero gold very much because it is very useful. In fact at first sight I have fallen in love with hero online gold. So no matter how much I have spent to buy hero gold, I never regret. Because of hero online money, I meet a lot of friends. So I never hesitate to buy hero money.
My friends and I like to buy habbo credits, because the habbo gold is very useful to upgrade equipment. Only your equipment becomes better, then you can win this game. In habbo coins, you can buy everything you want in this game. Tomorrow will be my birthday, so my friends promise to buy habbo gold as gifts. I am so happy. They understand me so well, cheap habbo credits is my favorite.

 
At April 27, 2009 4:09 AM, Blogger Rajesh G.S. said...

Please tell me how standby the WinCE O/S in the absence of external charger and resume when charging is ON?

 
At August 09, 2009 4:06 PM, Anonymous Anonymous said...

Online poker texas holdem free bonus. No veryfication no id needed no risk at all.
Play with other players free bankrolls no id required all possible $50 bankrolls.
starting bankrolls and capital $50 bonus poker. Stay away, get more cheap poker promotion.
once you have free starting poker capital from bankrollmob.
Over fifty sponsors dollars poker bonus darmowe pieniądze bez depozytu tylko czyszczenia za darmo.
Ale także bez wpłaty no i mansion bet most,poker darmowe free czyszczenia no risk, no deposit.
on line promo wow bonus sign up poker account for free huge and gift promotion.
free no deposit need id sign tapicerki. Dlatego zarabiaj online karcher no deposit information for poker,
more and free no deposit sponsorship no id verify from bet poker blog.
money czyszczenie poker sławków dąbrowa katowice chorzów. wyrzynarke meblową z karcher odkurzaczem, tanio i śmiesznie cheap. poker capital no id katowice ślask ale także online odbywa się szybko i dokładnie wow.
Promocje związane z czyszczenie wykładzin tapicerek jest na prawdę tanie online free.
Czyszczenie odbywa się u klienta.

 
At October 02, 2009 11:46 AM, Blogger pedro velasquez said...

In my two previous Pocket PC Power Management Series (Series 5 and Series 6), sportsbook I am trying to tackle the task of writing either continuously-running or periodically-running programs. I must admit such programs do not fit nicely into Microsoft's Windows Mobile-powered Pocket PCs, after having working on such applications myself for more than 1 year. Why? The fundamentally-flawed design of the Pocket PC devices prevent any good developer from developing really battery friendly programs.
When bet nfl Pocket PC device was initially designed, Microsoft made the decision that the device should never shutdown, probably by following Palm's design. Unless battery is removed, the device is always "alive". Unfortunately, Microsoft's OS is a trimmed version of its bigger brother Desktop Windows. Desktop Windows was never designed to be CPU or power http://www.enterbet.com friendly; in fact, desktop Windows seems to be proudly following the path of being more and more power hungry

 

Post a Comment

<< Home