[Solved].wav resource

Posts 1–8 of 8 · Page 1 of 1
[Solved].wav resource
1.) I added a 'bleep22.wav' to my resources.

How can I play the file from my resources on button event?
My.Computer.Audio.Play(My.Resources.RESOURCEFILE , AudioPlayMode.BackgroundLoop)
Location

Code:
Dim ES As New System.Media.SoundPlayer()  
  
ES.SoundLocation = "Wav File"   
ES.Load()  
ES.Play()
From Resources

Code:
My.Computer.Audio.Play(My.Resources."ResourceFile.wav", AudioPlayMode.BackgroundLoop)
From Embedded: (writing)

-- Add Existing Item --- > Choose the Wav

-- Right Click the File (.wav) Choose Properties

-- Changed Build action to embedded resource

Code to play the embedded file

Working out code , be back in a sec

Edit:

Okay Back

{In Name Space}

Code:
Imports System.Runtime.InteropServices
Imports System.Resources
Imports System.IO
Imports System
Directly under

Code:
Public Class Form1
Add:

Code:
        Public Class Winmm
        Public Const SND_ASYNC As UInt32 = 1
        Public Const SND_MEMORY As UInt32 = 4
Below That

Code:
        <DllImport("Winmm.dll")> _
        Public Shared Function PlaySound(ByVal data As Byte(), ByVal hMod As IntPtr, ByVal dwFlags As UInt32) As Boolean
        End Function
Below that

Code:
Public Shared Sub PlayWavResource(ByVal wav As String)

        Dim strNameSpace As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()
        Dim str As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream((strNameSpace & ".") + wav)
        If str Is Nothing Then
            Exit Sub
        End If
        Dim bStr As Byte() = New [Byte](str.Length - 1) {}
        str.Read(bStr, 0, CInt(str.Length))
        PlaySound(bStr, IntPtr.Zero, SND_ASYNC Or SND_MEMORY)
    End Sub
That is all you need to set it up to call wav files loaded in memory asynchronously.

To use it

On Button_Click Event (or anywhere) call the class

Code:
Win32.Winmm.PlayWavResource("YourwaveFile.wav")
IMO , Embedding is a better way to go , More professional

nextgen can you answer my pm on creating another .rexs resource file?
Yes, Now stay on topic
Thanks nextgen...

Using:

Code:
My.Computer.Audio.Play(My.Resources."ResourceFile.wav", AudioPlayMode.WaitToComplete)


Working fine.
Posts 1–8 of 8 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us