How to play an audio file (.wav) in WM 5.0 SP?

Hello. In project Visual Studio 2005, Visual Basic, Windows Mobile 5.0 Smartphone, Device Application, I have added an audio file, for example, drumpad-crash.wav.
On this forum, from Mr. Ilya Tumanov (posts 3313) there is the program, which in the given project I have written down in such kind:

Public Enum PlaySoundFlags
...
End Enum

Public Declare Function PlaySound Lib "CoreDll.dll"( _
ByVal szSound As String, ByVal hMod As IntPtr, _
ByVal flags As PlaySoundFlags) As Integer

In method Form1_Load, I have written down the next line:

PlaySound("..\..\drumpad-crash.wav, IntPtr.Zero, _
PlaySoundFlags.SND_LOOP)

At Start of the project, constantly (LOOP) sounds the signal, wich has been not connected to this file drumpad-crash.wav. The same signal sounds, if this line to write down so:

PlaySound("", IntPtr.Zero, _
PlaySoundFlags.SND_LOOP)

Inform, please, how to play an audio file (.wav) in this project
Beforehand thanks for the answer, Zharkov, Moskva, Russia.





Answer this question

How to play an audio file (.wav) in WM 5.0 SP?

  • ExtinctPencil

    Thanks for the answer. The use of sound files provides two variants.
    By the first variant, the file, for example, (.wav) is added in the project, then its property changes on Embedded Resource and only then the file is played with the help of method PlaySound.
    By the second variant, from external (in relation to the project) folder, for example, from OS Windows Mobile 5.0, the sound file all over again, apparently, should be converted in a binary format in stream, and only then to be play with the help of method PlaySound.
    By these known variants should be somewhere, for example, in the documentation on Windows Mobile 5.0 published typical programs. It is possible to ask you to result the links to these programs by two variants. Thanks.


  • MidNightQc

    Assuming it's not supported (which you can confirm in emulator’s forum), you can always debug on actual device. But since you’re getting system wave file (beep) played just fine, it’s pretty safe to conclude it’s supported. To be absolutely sure you can click on some system wave file and see if it would play.

    One more thing: make sure your wave file is in PCM format, that's the only one supported.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />



  • Learning VB

    OK, now I figured out what you're trying to do in your code - you believe if you attach resource name to the EXE name (which would get you something like “foo.exe.bar.wav”) and pass that to the PlaySound, it would be able to play it

    I'm afraid it won't work - SND_FILENAME is intended for wave files on file system only, so PlaySound would try to open whatever combined file name you've got literally (e.g. “foo.exe.bar.wav”) instead of loading resource from the exe as you want it to. Since there’s no such file (“foo.exe.bar.wav”) on a file system, all you get is beep.

    Playing resources is possible but done differently. Let me repeat myself:

    "If you want to play embedded resource in managed application: get stream to that resource, load it into byte array and pass that array to PlaySound, use SND_MEMORY flag."

    As to “write program correctly”, it’s up to you to do that. I’m sure you’ll figure it out eventually.



  • progame

    If you want to play file on a file system: pass proper file name to PlaySound and use SND_FILENAME flag.

    If you want to play embedded resource in managed application: get stream to that resource, load it into byte array and pass that array to PlaySound, use SND_MEMORY flag.



  • Steven P.

    I have written this program on C#, and instead of playing a file of a format (.wav) have heard the same single signal. Therefore I have drawn a conclusion, that projects of type Visual Studio 2005, Visual Basic, Windows Mobile 5.0 Smartphone, Device Application and type Visual Studio 2005, Visual C#, Windows Mobile 5.0 Smartphone, Device Application do not support the playing these sound files of a format (.wav).

    Thanks.

  • PublicError

    I remind logic of the program on playing a sound file of format (.wav).

    In project Visual Studio 2005, Visual Basic, Windows Mobile 5.0 Smartphone, Device Application,

    from the folder C, WINDOWS, Media, we add (Project, Add Existing Item) a sound file, for example, tada.wav,

    then Property of this file, we change on Embedded Resource.

    Further we write down the above mentioned program with the next final line:

    PlaySound(myName_of_project + ".tada.wav", _

    IntPtr.Zero, _

    Fix(PlaySoundFlags.SND_ASYNC Or _

    PlaySoundFlags.SND_FILENAME))

    After Start of this project, we hear a single signal of type Beep.

    Inform, please, that such the “PCM format” for a sound file of the format (.wav), and how in this program to execute your recommendation: “make sure your wave file is in PCM format”

    Thanks.

  • Barryrocks

    To ; Valery Zharkov

    I am also doing the same application using vb2005 in WM5 .

    Can i get the Example code from you .Because i am very new to this subject vb2005 ( beginner Learner ).

    Thank YOU



  • RussP

    Thanks for the link, but this link to me it is known. In the previous post, I have resulted the typical program, which is applied at programming the mobile devices, when in the project is added any file, and property of this file changes on Embedded Resource. You consider: “...you’re doing it wrong”.
    Write, please, this program correctly.
    Thanks.



  • Karl Hulme

    You'd need to fix path to the file - devices don't have relative paths (which you're trying to use), drive letters and can't see desktop's hard drives. Also you can only play PCM (uncompressed) WAV files.



  • dba_sql

    I'm here to help you in doing your job, not to do it for you. I want developers to understand how code works and be able to fix and change it as needed, not just mindlessly cut and paste somebody's else code without a clue on how it works.

    So here’s my logic – I want you to become a better developer who can actually do the job. I’ve explained how to play wave resources, now I expect you to do your job and implement it using existing code as a base. Good luck.



  • abcdefgqwerty2

    Thanks for the answer. By the first variant, in project Visual Studio 2005, Visual Basic, Windows Mobile 5.0 Smartphone, Device Application, is added the file, for example, tada.wav, then its property changes on Embedded Resource.

    Further the program very simple:

    Imports System.Reflection

    Public Class Form1

    Public Enum PlaySoundFlags

    SND_ALIAS = &H10000

    SND_FILENAME = &H20000

    SND_RESOURCE = &H40004

    SND_SYNC = &H0

    SND_ASYNC = &H1

    SND_NODEFAULT = &H2

    SND_MEMORY = &H4

    SND_LOOP = &H8

    SND_NOSTOP = &H10

    SND_NOWAIT = &H2000

    SND_VALIDFLAGS = &H17201F

    SND_RESERVED = &HFF000000

    SND_TYPE_MASK = &H170007

    End Enum


    Public Declare Function PlaySound Lib _
    "CoreDll.dll" ( _

    ByVal szSound As String, ByVal hMod As IntPtr, _

    ByVal flags As PlaySoundFlags) As Integer


    Private Sub Form1_Load( _
    ByVal
    sender As System.Object, _

    ByVal e As System.EventArgs) Handles MyBase.Load

    Dim myAssembly As Assembly = _

    Assembly.GetExecutingAssembly()

    Dim myAssemblyName As AssemblyName = _

    myAssembly.GetName()

    Dim myName_of_project As String = _
    myAssemblyName.Name

    PlaySound(myName_of_project + ".tada.wav", _

    IntPtr.Zero, _

    Fix(PlaySoundFlags.SND_ASYNC Or _

    PlaySoundFlags.SND_FILENAME))

    End Sub

    End Class

    However in this variant is played one sound such as Beep, irrespective of file tada.wav, only the file tada.wav is not played.

    Inform, please, why in this project does not play this file tada.wav

    Thanks.

  • uasound

    Before to debug the program, it is necessary to find out all over again, this Windows Mobile 5.0 Smartphone Emulator (from the project Visual Studio 2005, Visual Basic, Windows Mobile 5.0 Smartphone, Device Application) supports the playing of sound files (.wav) or does not supports If not supports, the program to debug it is impossible.

    Inform, please, where to documentation it is written, what this Windows Mobile 5.0 Smartphone Emulator supports the playing of sound files

    Thanks.

  • Annihil8

    I do not understand your logic. Above I have written the program. You count this program wrong. Instead of the common reasonings, bring, please, to this program of change, and I, and all interested persons will check up, works whether my program with your changes or does not work

    Thanks.

  • manick312938

    That's probably because path to the wave file is incorrect – PlaySound would beep if it can’t find the file.

    If you’re trying to append application path to the wave file name - you’re doing it wrong. See this on how to do it right.



  • How to play an audio file (.wav) in WM 5.0 SP?