Windows Mobile 6.1 Play Sound

G Scott
G Scott used Ask the Experts™
on
Howdy. I have seen a few solutions for playing a sound file on a mobile device that is running WM6+.  One of the common denominators is this line:

Import System.Media

What do I have to do to get that option? I type that and it doesn't allow me to do that. I am very green in .NET (well versed in VBA).

I am using an Intermec CK3, if that matters. I just want to play a sound so that the user knows everything is ok or not ok. (ding/buzz, respectively)

Can someone help me out with this?  I only know/use VB..C Sharp/++ scares me.

Thanks for any help you can give me.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
The .NET Compact Framework version 3.5 supports the SoundPlayer class, which provides an intuitive API for playing sounds in applications such as casual games. To enable casual game development, the .NET Compact Framework pushes each SoundPlayer object through WaveOut, which is the audio rendering filter in Windows Embedded CE and Windows Mobile powered devices. This filter enables the device to mix sounds.

The .NET Compact Framework 3.5 supports the following types:

SoundPlayer

SystemSound

SystemSounds

For a code example that demonstrates how to use this feature, see SoundPlayer.

The differences between the .NET Compact Framework implementation and the desktop implementation of SoundPlayer include the following:

The .NET Compact Framework supports only PCM-encoded files (uncompressed sound).

On a device, more than one sound can play at the same time. On the desktop, only the last sound plays.

On a device, the application will not terminate until playback is completed. If a sound is playing in the application loop, the application will never exit. To avoid this situation, you can explicitly terminate playback by calling the Stop or Dispose method on the SoundPlayer object.

Reference From:


An alternative way to play Beeps is to use the "coredll.dll" and a file you place at your device.

Dim ExePath As String

Private Declare Function PlaySound Lib "coredll.dll" Alias "PlaySound" _
(ByVal lpszName As String, _
ByVal hMod As IntPtr, _
ByVal hwFlags As Integer) As Integer
Private Sub Beep_Test()
PlaySound(ExePath + "test.wav", IntPtr.Zero, &H1 Or &H20000)
End Sub

Commented:
btw, the system.media interface is not presented in the .Net Compact Framework
Most Valuable Expert 2012
Top Expert 2014

Commented:
>What do I have to do to get that option? I type that and it doesn't allow me to do that.

Have you added a reference to system.media?

@asfahani
Are you negating your first comment? SoundPlayer, SystemSound, and SystemSounds classes are within System.Media namespace.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Commented:
no i mean .Net Compact Framework 2.0
If you are using .Net CF 3.5 then you have no problem.
What visual studio are you using?

Author

Commented:
I am using VS2008 with CF3.5. When I try to add a reference the .NET list is very short:

Screen of References
I don't see anything with the word Media, or similar to that at all.

asfahani - I tried your code but I am still just getting a beep instead of the wav file that I want to have played.
Commented:
since you are using intermec, try to add reference as follow:
select ADD REFERENCE->Browse tab->Look in the box & point to
Developer Library->DOT Net Assemblies->CF3.5->Intermec.media.CF3.5.dll
where developer library is the folder where intermec drivers and assemblies reside
That will get you the reference you need to make it work.
If you didn't find that or a dll related to it then try using below code, deploy it in a class. It relies on the aygshell.dll which exists in all windows mobile devices.
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Threading
Imports System.Runtime.InteropServices

Namespace WindowsMobile.Utilities
    Public Class SoundPlayer
        <DllImport("aygshell.dll")> _
        Private Shared Function SndOpen(ByVal pszSoundFile As String, ByRef phSound As IntPtr) As UInteger
        End Function

        <DllImport("aygshell.dll")> _
        Private Shared Function SndPlayAsync(ByVal hSound As IntPtr, ByVal dwFlags As UInteger) As UInteger
        End Function

        <DllImport("aygshell.dll")> _
        Private Shared Function SndClose(ByVal hSound As IntPtr) As UInteger
        End Function

        <DllImport("aygshell.dll")> _
        Private Shared Function SndStop(ByVal SoundScope As Integer, ByVal hSound As IntPtr) As UInteger
        End Function

        <DllImport("aygshell.dll")> _
        Private Shared Function SndPlaySync(ByVal pszSoundFile As String, ByVal dwFlags As UInteger) As UInteger
        End Function

        Const SND_SCOPE_PROCESS As Integer = &H1


        Private mySoundLocation As String = String.Empty

        Public Property SoundLocation() As String
            Get
                Return mySoundLocation
            End Get
            Set(ByVal value As String)
                mySoundLocation = value
            End Set
        End Property

        Private mySound As IntPtr = IntPtr.Zero
        Private myThread As Thread = Nothing

        Private Function playsound()
            While True
                SndPlaySync(mySoundLocation, 0)
            End While
        End Function

        Public Sub PlayLooping()
            myThread = New Thread(AddressOf playsound)
			myThread.Start()
		End Sub

        Public Sub Play()
            SndOpen(mySoundLocation, mySound)
            SndPlayAsync(mySound, 0)
        End Sub

        Public Sub [Stop]()
            If myThread IsNot Nothing Then
                SndStop(SND_SCOPE_PROCESS, IntPtr.Zero)
                myThread.Abort()
                myThread = Nothing
            End If
            If mySound <> IntPtr.Zero Then
                SndStop(SND_SCOPE_PROCESS, IntPtr.Zero)
                SndClose(mySound)
                mySound = IntPtr.Zero
            End If
        End Sub

#Region "IDisposable Members"

        Public Sub Dispose()
            [Stop]()
        End Sub

#End Region
    End Class
End Namespace

Open in new window


from the place you want to test the sound use this code.

        Dim test As WindowsMobile.Utilities.SoundPlayer
        test = New WindowsMobile.Utilities.SoundPlayer()
        test.SoundLocation = "testfilelocation"
        test.Play()

Open in new window


 this should work for all windows mobile devices. I use motorola and it works fine.

Regarding the previous code i gave you, Make sure to specify the correct location to the file test.wav,  because if the system fails in locating the file it will play the default beep sound.

Author

Commented:
I installed a few SDKs from Intermec, but was never able to find that DLL. However, the other solution you gave worked great!! Thank you so much; I truly appreciate it.

Commented:
glad to help you, welcome anytime!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial