Jump to content
Xtreme .Net Talk

Recommended Posts

  • Leaders
Posted
If you have searched the object browser or MSDN you would discover that there are no sound file related classes in the framework, period. For some crazy reason, Microsoft did not anticipate that people might want to play sounds. What you need is the Windows API. There are different API viewers (you can find them with google) or you can just search Google for Windows APIs related to sound. And just a note, as suggested in my sig, this kind of info is generally easily available in either the object browser, MSDN, or Google.
[sIGPIC]e[/sIGPIC]
Posted

I figured out a pretty simple way to have a .WAV file from your computer play on .NET at a spot in the program.

Insert the following code where you want the sound to play within the program:

 

My.Computer.Audio.Play("INSERT YOUR FILENAME HERE", AudioPlayMode.Background) 'be sure to include the quotes around the filename

Posted

Play a sound with VB.Net

 

Hi try this:

 

Add a new class and name it clsSound.

 

Paste in the following code overwriting all existing code in the class:

 

Imports System

Imports System.Runtime.InteropServices

Imports System.Resources

Imports System.IO

Public Class clsSound

 

Private Shared SND_ASYNC As Integer = 1

Private Shared SND_MEMORY As Integer = 4

 

Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As Byte(), ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer

Public Shared Sub PlayWavResource(ByVal strWav As String)

 

Dim strNameSpace As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()

 

 

'<<CM Get the resource into a stream

Dim resourceStream As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(strNameSpace + "." + strWav)

If resourceStream Is Nothing Then

Exit Sub

End If

 

'<<CM Bring the stream into a byte array

Dim wavData As Byte()

ReDim wavData(CInt(resourceStream.Length))

resourceStream.Read(wavData, 0, CInt(resourceStream.Length))

 

'<<CM Play the resource

PlaySound(wavData, 0, SND_ASYNC Or SND_MEMORY)

End Sub

End Class

 

Next, add the .wav file to the project and in the file properties window set the build action of the file to compiled resource. This is important!

 

To play the file all you have to do is the following. In this example the code will play the embedded resource file Error.wav

 

Dim Sound As New clsSound

 

Sound.PlayWavResource(Error.wav)

 

post back if you have any problems

 

cheers :rolleyes:

 

Code mechanic

Posted
Hi try this:

 

Add a new class and name it clsSound.

 

Paste in the following code overwriting all existing code in the class:

 

Imports System
Imports System.Runtime.InteropServices
Imports System.Resources
Imports System.IO
Public Class clsSound

   Private Shared SND_ASYNC As Integer = 1
   Private Shared SND_MEMORY As Integer = 4

   Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As Byte(), ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer
   Public Shared Sub PlayWavResource(ByVal strWav As String)

       Dim strNameSpace As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()


       '<<CM Get the resource into a stream
       Dim resourceStream As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(strNameSpace + "." + strWav)
       If resourceStream Is Nothing Then
           Exit Sub
       End If

       '<<CM Bring the stream into a byte array
       Dim wavData As Byte()
       ReDim wavData(CInt(resourceStream.Length))
       resourceStream.Read(wavData, 0, CInt(resourceStream.Length))

       '<<CM Play the resource
       PlaySound(wavData, 0, SND_ASYNC Or SND_MEMORY)
   End Sub
End Class

 

Next, add the .wav file to the project and in the file properties window set the build action of the file to compiled resource. This is important!

 

To play the file all you have to do is the following. In this example the code will play the embedded resource file Error.wav

 

Dim Sound As New clsSound

 

Sound.PlayWavResource(Error.wav)

 

post back if you have any problems

 

cheers :rolleyes:

 

Code mechanic

 

The very reason .NET 2.0 is better than 1.1. :cool:

  • 1 year later...
Posted

I know this is ancient, but I was searching for how to do a simple wav playback myself (DirectSound or whatever it is I think would be sledge hammer for what I'm doing)....

 

So for those of you who prefer C# over VB, you can use System.Media.SoundPlayer for this rather than the 'My' namespace...thanks Lutz!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...