Monday, July 9, 2007

Accessing Windows Mobile 6.0 Sound API's through .NETCF

A new set of APIs were introduced in Windows Mobile 6 to make it easier to manage and play sound. The new API's support playing sounds in a variety of formats that Windows Media Player supports

These API's are really easy to use. You can play a sound file with a single function call. Let's try to do that through .NETCF by P/Invoking SndPlaySync(LPCTSTR pszSoundFile, DWORD dwFlags).


[DllImport("aygshell.dll")]
static extern uint SndPlaySync(string pszSoundFile, uint dwFlags);

void PlaySound() {
  SndPlaySync("\\Storage Card\\Intro.mp3", 0);
}


In the previous sample, we are playing a sound file synchronously. Now, this is interesting in a way that its very very easy to play an audio file. But what really gets interesting is that the new Sound API provides methods for playing sound files asynchronously.

To play audio files asynchronously, we will need to call 4 methods from the Sound API.

  SndOpen(LPCTSTR pszSoundFile, HSOUND* phSound)
  SndPlayAsync(HSOUND hSound, DWORD dwFlags)
  SndClose(HSOUND hSound)
  SndStop(SND_SCOPE SoundScope, HSOUND hSound)

Let's start by declare our P/Invokes


[DllImport("aygshell.dll")]
static extern uint SndOpen(string pszSoundFile, ref IntPtr phSound);

[DllImport("aygshell.dll")]
static extern uint SndPlayAsync(IntPtr hSound, uint dwFlags);

[DllImport("aygshell.dll")]
static extern uint SndClose(IntPtr hSound);

[DllImport("aygshell.dll")]
static extern uint SndStop(int SoundScope, IntPtr hSound);


Now that we have our P/Invokes ready. Let's start playing with the Sound API in .NETCF. In the sample below, the application will play the audio file Intro.mp3 located in the Storage Card. To play an Audio file asynchronously, we will first need a handle to the audio file. We use SndOpen(string, IntPtr) to accomplish that. Once we have the handle to the audio file, we can call SndPlayAsync(IntPtr, int) to start playing the audio file. To stop playing the audio we just have to close the handle and call SndStop(SND_SCOPE_PROCESS, IntPtr.Zero) to stop the playback of the sound.


IntPtr hSound = IntPtr.Zero;
const string AUDIO_FILE = "\\Storage Card\\Intro.mp3";
const int SND_SCOPE_PROCESS = 0x1;

void Play() {
  SndOpen(AUDIO_FILE, ref hSound);
  SndPlayAsync(hSound, 0);
}

void Stop() {
  SndClose(hSound);
  SndStop(SND_SCOPE_PROCESS, IntPtr.Zero);
}


How cool is that? You can now easily add some cool sound effects to your application. Maybe even use the Sound API for one of those annoying startup sounds!

30 comments:

Sabu C.Alex said...

hi Christian Helle

I am a windows mobile developer
This blog helped me a lot.

Thank to you

Sabu C.Alex

Christian Resma Helle said...

I'm glad to be of service...

Sven said...

Christian...I am a new mobile developer and I want to somehow write a new media player or somehow alter the current one to add a new feature. I want to be able to simply set a start point and a stop point when a song is playing in order to loop it continuously. This will be used to learn songs for the band I am in when it is nice to be able to loop a song section over and over til I know the part. Currently this requires a laptop using some custom software. It would be nice to be able to do this on my Samsung Blackjack cell phone. Any help you can provided is certainly appreciated.

Anonymous said...

Hi Christian

I am trying to access sound APi's declared in Windows mobile 6.0 and i am getting the following errors while compiling

error CS0103: The name 'SndPlaySnyc' does not exist in the current context.

Any idea why i am getting the above error??. Thanks in advance

Christian Resma Helle said...

Did you declare you P/Invokes? Can you post your code?

If it's a compile error then we can easily pin point what is wrong

Manchem Sandeep said...

hii Christian Helle

I am newbie to windows mobile programming. I want to convert text to speech so that I can create some application to read SMS and Email. Are there any TTS engines available that can be used on windows mobile 6 ???
Any help in this regard would be really great.

Thank you
Sandeep Manchem

Anonymous said...

Hey, nice and clean.. thanks :)

Ninad said...

How to mute microphone using this API? Is its possible using this API?

Pirobo2008 said...
This comment has been removed by the author.
Pirobo said...

hi christian im using these functions and it works great, the thing is i want to do a little personal media player for me just to test it out, the thing is i cannot pause the music, is there another function to apuse or how does it tdo that,

thx

Anonymous said...

Hi Christian,

This Article is great! Thanks a lot i'm using this function and it works great.
First time i tried it, i got an compile error, because i did just copy&paste and you wrote SndPlaySnyc instead of SndPlaySync in the function call. I think this is the same problem a anonymous Poster had before.

Again: Thank you, i love this fucntion!

Thzamus

Christian Resma Helle said...

Thanks for pointing that out! I'll update the article immediately!

Danielle said...

Hello, i have the problem that the sound only play on the pocket pc but not in the windows mobile emulator.

Have anybody a solution to play the sound in the emulator?
I need for a presentation.

Thanks Danielle

Christian Resma Helle said...

This should only work on the Windows Mobile 6.0 Emulators, The API does not exist in the earlier versions.

Danielle said...

Hello, thanks now i can play sounds on my device :)
is there a possible to check if there is still a sound playing ?
i will play a song and after this song is finished i will start again ...

i hope u can help me

Anonymous said...

Hi Christian Helle

thanks for your posting.

By the way, how can I change audio volume??

please give me a help..

Amar said...

hi,

can i use these api's to play sound while the call is in progress.

help me out..

Thanks,
Amar

Christian Resma Helle said...

Hi Amar,

Good question, I actually haven't tried. In all my phones, all media applications are stopped when there is an incoming voice call.

Regards,
Christian

Anonymous said...

Hi Christian

This post was very helpful. I face a problem when implementing the code. Everytime i call the Stop() function, an error will happen.

The error is as follows:

EvntType: WinCE501bExceptio
App Name: app1.exe
.
.
.
ModName: aygshell.dll

Thanks
Jack

Chris Mitsis said...

I am too young to understand English like that and i live in Greece please send me an e-mail with a project like that: when i click at a button play a sound. My e-mail: mitsis555@hotmail.com thank you

kimberly said...

viagra online
generic viagra

buy generic viagra said...

Hi friend,
You did really a great job. I found your blog very interesting and very informative. I think your blog is great informaiton source & I like your way of writing and explaining the topics. Keep it up. I'm going to follow your blog.

Anonymous said...

I am new in windows mobile. I need to make an app on windows mobile device that records a wavefile. A way is to do it wit directx or direct sound? I have never worked with those librarie. Can you give me the source code? After I play the wave file I need to decode it. I have the project that works in visual c # on windows...

Christian Resma Helle said...

You can use the Waveform Audio Interface.

Here's an article describing the process:
http://msdn.microsoft.com/en-us/library/aa446573.aspx

Anonymous said...

ok. Thank you very much.
The idea is that: I have a textbox where I include a number: 01234. Using the waveformat, (subchunks, header..) I thansform that number in a .wav ( like number.wave). I take that number.wave and decode it and obtain the numbers: 01234. The code I made in windoes for decode doesn;t match with windows mobile. So I need one that works on windows mobile. Thank you for help, if you have other suggestion I will be glad.
Thank you again. Mary

Record said...

can I use this record on windows mobile device (emulator)?
http://justlikeamagic.com/2010/03/23/creating-a-sound-recorder/

Angelica said...

do you know how to get the root folder from windows mobile? How to access 'my device'folder for example

Anonymous said...

Hi Christian,

Thank you for your informations)

I would know the way to disable "all sound" in WM 6.X

I'm creating a little app (just to fun) for me...Or for example after installing the app, it go to disable audio calls...I mean: i call a fiend but i can't hear nothing..ringing, voice..Is possible to that?

Thank you Christian)

Robert

viagra for sale said...

Thanks for a marvelous posting! I seriously enjoyed reading it, you can be a great author

HOTELS in LOS ANGELES CA said...

fairly an interesting post. Your blog is very interesting and I like to read it! Thanks for sharing. Regards.