wav

DR00ME

Well-known member
Joined
Feb 6, 2004
Messages
169
Location
Finland
I got this folder in my project and it contains some .wavs ... how do I play them ? I heard the keyword would be ResourceManager class which should be newed or something ?
So any help would be appreciated!
 
There are several ways. Heres one, using SndPlaySound API
Code:
   API Declaration
    Declare Auto Function sndPlaySound Lib "WINMM.DLL" (ByVal FileName As String, ByVal Options As Int32) As Int32

       For Info
      API Call Flags for sndPlaySound
    Const SND_SYNC As Long = &H0  synchronize playback 
    Const SND_ASYNC As Long = &H1   played async
    Const SND_NODEFAULT As Long = &H2   No default 
    Const SND_LOOP As Long = &H8   loop the wave 
    Const SND_NOSTOP As Long = &H10   dont stop current sound if one playing
    Const SND_NOWAIT As Long = &H2000  Do not wait if the sound driver is busy. 
    Const SND_ALIAS As Long = &H10000   Play a Windows sound (such as SystemStart, Asterisk, etc.). 

  eg of calling it
 Public Sub PlayAWave(ByVal Soundfile As String, Optional ByVal MaxLoops As Integer = 1)
        Dim i As Integer
        For i = 1 To MaxLoops
              Play Async - ie.  Dont lock up the application
            sndPlaySound(Soundfile, SND_ASYNC)
    End Sub

Some people prefer to use Windows Media Player in projects, but Ive found this API to be OK for WAVs.

kas
 
My problem is what do I put in the "soundfile" place... how do I link the embedded soundfile which is in a folder inside the project to that playerfunction...

lets say the foldername would be MYSOUND and the file name 1.wav.

Code:
           Dim FileName As String = "1.wav"
            Dim MyStream As Stream
            Dim oAssembly As Reflection.Assembly = MyClass.GetType.Assembly
            Dim nSpace As String = oAssembly.GetName.Name.ToString
            MyStream = oAssembly.GetManifestResourceStream(nSpace + "." + "wav")
            Dim bStr(MyStream.Length) As Byte

            MyStream.Read(bStr, 0, Int(MyStream.Length))


            PlaySound(MyStream, 0, SND_ASYNC Or SND_MEMORY)

IT is something like this but this doesnt work.... :(
 
Last edited by a moderator:
[VB] Dim MyStream As Stream
Dim oAssembly As Reflection.Assembly = MyClass.GetType.Assembly
Dim nSpace As String = oAssembly.GetName.Name.ToString
MyStream = oAssembly.GetManifestResourceStream(nSpace + "." + "1.wav")
Dim bStr(MyStream.Length) As Byte

Dim ClickSound() As Byte
ReDim ClickSound(CType(MyStream.Length, Integer))

MyStream.Read(ClickSound, 0, Int(CType(MyStream.Length, Integer)))


PlaySound(ClickSound, IntPtr.Zero, SND_ASYNC Or SND_MEMORY)[/VB]


WOOHOO and so it works!!!!!!!
 
make sure you use the PlaySound Api , rather than sndPlaySound.
in the time of me knocking an example up , you have answered your question i guess , but heres the example i knocked up...
Code:
Public Class _Win32
    Public Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As Byte(), ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer
    Public Const SND_ASYNC As Integer = &H1
    Public Const SND_MEMORY As Integer = &H4
End Class

/// then in a button click event of your Form ...
        Dim strPath As String = Application.ProductName & ".online.wav" /// must be an embedded .Wav files valid path here
        Dim sStream As IO.Stream = Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(strPath)
        Dim strLength As Integer = sStream.Length
        Dim sndByte(strLength) As Byte

        sStream.Read(sndByte, 0, strLength)
        sStream.Close()

        _Win32.PlaySound(sndByte, 0, _Win32.SND_ASYNC Or _Win32.SND_MEMORY)
 
n1, now we got 2 nice versions of it :D thx

Exept my version got 2 x int conversion = not needed ...and ReDim = not needed....
 
Last edited by a moderator:
Sorry its in c#
but the only important line of code is the "using(..."
This is using managed dx9
Code:
using Microsoft.DirectX.AudioVideoPlayback;

namespace FOoBar
{
	class Foo
	{
		[STAThread]
		static void Main(string[] args)
		{
			//open the wav file and play it
			using(Audio wavFile = new Audio(@"C:\myWavFile.wav", true))
			{
			}
		}
	}
}
 
sorry if im digging this back up , but wait for the next version of visual studio to arrive ;) it has a class called System.Windows.Forms.Sound , all you need to do is this ...
Code:
        /// System.Windows.Forms.Sound ( visual studio Whidbey )
        Dim strPath As String = "WindowsApplication1.online.wav" /// path to a valid embedded .wav file
        Dim snd As New Sound(Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(strPath))
        snd.Play()
 
Back
Top