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 thoughts on “Accessing Windows Mobile 6.0 Sound API’s through .NETCF

  1. hi Christian Helle

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

    Thank to you

    Sabu C.Alex

  2. 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.

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. Hi Christian Helle

    thanks for your posting.

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

    please give me a help..

  10. 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

  11. 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

  12. 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.

  13. 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…

  14. 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

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

  16. 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

Leave a Reply

Your email address will not be published. Required fields are marked *