g00r00
asked on
Background music in vb .net
How to play background music using VB.NET? I mean play backround music with out any dll files needed to my application to be running!(cause i tried once, and it works but it creates 3 dll files with it, and my app wasnt working with out those files) Also how can I build in music file in my application?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You have to play music n the different thread
'Form
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PlayS("c:\windows\media\ta da.wav")
End Sub
Private Sub PlayS(ByVal sound As String)
Dim thread As New SoundThread()
Dim mThread As New System.Threading.ThreadSta rt(Address Of thread.newThread)
Dim oThread As New System.Threading.Thread(mT hread)
thread.sound = sound
oThread.Start()
End Sub
End Class
Class SoundThread
Public sound As String
Sub newThread()
PlaySound(sound, 0, 0)
End Sub
End Class
'Module
Module Module1
Public Declare Function PlaySound Lib "winmm.dll" (ByVal filename As String,ByVal hmodule As Long, ByVal dword As Integer) As Boolean
End Module
'Form
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PlayS("c:\windows\media\ta
End Sub
Private Sub PlayS(ByVal sound As String)
Dim thread As New SoundThread()
Dim mThread As New System.Threading.ThreadSta
Dim oThread As New System.Threading.Thread(mT
thread.sound = sound
oThread.Start()
End Sub
End Class
Class SoundThread
Public sound As String
Sub newThread()
PlaySound(sound, 0, 0)
End Sub
End Class
'Module
Module Module1
Public Declare Function PlaySound Lib "winmm.dll" (ByVal filename As String,ByVal hmodule As Long, ByVal dword As Integer) As Boolean
End Module
Here is a nice class to play wav file !
Just execute it in a separate thread!
Notice that it includes all the constants of "PlaySound" --> Customize your calls !
Imports System
Imports System.Runtime.InteropServ ices
Imports System.Resources
Imports System.IO
Public Class WavPlayer
'Declare the Playsound API to play sounds
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySound" _
(ByVal data() As Byte, ByVal hMod As IntPtr, _
ByVal dwFlags As Int32) As Boolean
'the dwFlages can hold one of the following values :
'SND_SYNC
' Synchronous playback of a sound event. PlaySound returns after
' the sound event completes.
Private Const SND_SYNC As Integer = 0 '&H0
'SND_ASYNC
' The sound is played asynchronously and PlaySound returns immediately
' after beginning the sound. To terminate an asynchronously played
' waveform sound, call PlaySound with pszSound set to NULL.
Private Const SND_ASYNC As Int32 = 1 '&H1
'SND_NODEFAULT
' No default sound event is used. If the sound cannot be found, PlaySound
' returns silently without playing the default sound.
Private Const SND_NODEFAULT As Integer = 2 '&H2
'SND_MEMORY
' A sound event’s file is loaded in RAM. The parameter specified by pszSound
' must point to an image of a sound in memory.
Private Const SND_MEMORY As Integer = 4 '&H4
'SND_LOOP
' The sound plays repeatedly until PlaySound is called again with
' the pszSound parameter set to NULL. You must also specify the SND_ASYNC
' flag to indicate an asynchronous sound event.
Private Const SND_LOOP As Integer = 8 '&H8
'SND_NOSTOP
' The specified sound event will yield to another sound event that is
' already playing. If a sound cannot be played because the resource
' needed to generate that sound is busy playing another sound, the function
' immediately returns FALSE without playing the requested sound.
' If this flag is not specified, PlaySound attempts to stop the currently
' playing sound so that the device can be used to play the new sound.
Private Const SND_NOSTOP As Integer = 16 '&H10
'SND_PURGE
' Sounds are to be stopped for the calling task. If pszSound is not NULL,
' all instances of the specified sound are stopped. If pszSound is NULL,
' all sounds that are playing on behalf of the calling task are stopped.
' You must also specify the instance handle to stop SND_RESOURCE events.
Private Const SND_PURGE As Integer = 64 '&H40
'SND_APPLICATION
' The sound is played using an application-specific association.
Private Const SND_APPLICATION As Integer = 128 '&H80
'SND_NOWAIT
' If the driver is busy, return immediately without playing the sound.
Private Const SND_NOWAIT As Integer = 8192 '&H2000
'SND_ALIAS
' The pszSound parameter is a system-event alias in the registry
' or the WIN.INI file. Do not use with either SND_FILENAME or SND_RESOURCE.
Private Const SND_ALIAS As Integer = 65536 '&H10000
'SND_FILENAME
' The pszSound parameter is a filename.
Private Const SND_FILENAME As Integer = 131072 '&H20000
'SND_RESOURCE
' The pszSound parameter is a resource identifier; hmod must identify
' the instance that contains the resource.
Private Const SND_RESOURCE As Integer = 262148 '&H40004
'SND_ALIAS_ID
' The pszSound parameter is a predefined sound identifier.
Private Const SND_ALIAS_ID As Integer = 1114112 '&H110000
'------------------------- ---------- ---------- --
'<summary></summary>
'<param name=""></param>
'<returns></returns>
Public Shared Function PlayWavResource(ByVal wavStream As Stream) As Boolean
If wavStream Is Nothing Then Throw New ArgumentNullException("wav Stream")
If wavStream.CanRead Then
Dim StreamLength As Integer = Convert.ToInt32(wavStream. Length)
'bring stream into a byte array
Dim byteStream(StreamLength) As Byte
wavStream.Read(byteStream, 0, StreamLength)
'play the resource
Return PlaySound(byteStream, IntPtr.Zero, SND_ASYNC Or SND_MEMORY)
End If
End Function
End Class
Just execute it in a separate thread!
Notice that it includes all the constants of "PlaySound" --> Customize your calls !
Imports System
Imports System.Runtime.InteropServ
Imports System.Resources
Imports System.IO
Public Class WavPlayer
'Declare the Playsound API to play sounds
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySound" _
(ByVal data() As Byte, ByVal hMod As IntPtr, _
ByVal dwFlags As Int32) As Boolean
'the dwFlages can hold one of the following values :
'SND_SYNC
' Synchronous playback of a sound event. PlaySound returns after
' the sound event completes.
Private Const SND_SYNC As Integer = 0 '&H0
'SND_ASYNC
' The sound is played asynchronously and PlaySound returns immediately
' after beginning the sound. To terminate an asynchronously played
' waveform sound, call PlaySound with pszSound set to NULL.
Private Const SND_ASYNC As Int32 = 1 '&H1
'SND_NODEFAULT
' No default sound event is used. If the sound cannot be found, PlaySound
' returns silently without playing the default sound.
Private Const SND_NODEFAULT As Integer = 2 '&H2
'SND_MEMORY
' A sound event’s file is loaded in RAM. The parameter specified by pszSound
' must point to an image of a sound in memory.
Private Const SND_MEMORY As Integer = 4 '&H4
'SND_LOOP
' The sound plays repeatedly until PlaySound is called again with
' the pszSound parameter set to NULL. You must also specify the SND_ASYNC
' flag to indicate an asynchronous sound event.
Private Const SND_LOOP As Integer = 8 '&H8
'SND_NOSTOP
' The specified sound event will yield to another sound event that is
' already playing. If a sound cannot be played because the resource
' needed to generate that sound is busy playing another sound, the function
' immediately returns FALSE without playing the requested sound.
' If this flag is not specified, PlaySound attempts to stop the currently
' playing sound so that the device can be used to play the new sound.
Private Const SND_NOSTOP As Integer = 16 '&H10
'SND_PURGE
' Sounds are to be stopped for the calling task. If pszSound is not NULL,
' all instances of the specified sound are stopped. If pszSound is NULL,
' all sounds that are playing on behalf of the calling task are stopped.
' You must also specify the instance handle to stop SND_RESOURCE events.
Private Const SND_PURGE As Integer = 64 '&H40
'SND_APPLICATION
' The sound is played using an application-specific association.
Private Const SND_APPLICATION As Integer = 128 '&H80
'SND_NOWAIT
' If the driver is busy, return immediately without playing the sound.
Private Const SND_NOWAIT As Integer = 8192 '&H2000
'SND_ALIAS
' The pszSound parameter is a system-event alias in the registry
' or the WIN.INI file. Do not use with either SND_FILENAME or SND_RESOURCE.
Private Const SND_ALIAS As Integer = 65536 '&H10000
'SND_FILENAME
' The pszSound parameter is a filename.
Private Const SND_FILENAME As Integer = 131072 '&H20000
'SND_RESOURCE
' The pszSound parameter is a resource identifier; hmod must identify
' the instance that contains the resource.
Private Const SND_RESOURCE As Integer = 262148 '&H40004
'SND_ALIAS_ID
' The pszSound parameter is a predefined sound identifier.
Private Const SND_ALIAS_ID As Integer = 1114112 '&H110000
'-------------------------
'<summary></summary>
'<param name=""></param>
'<returns></returns>
Public Shared Function PlayWavResource(ByVal wavStream As Stream) As Boolean
If wavStream Is Nothing Then Throw New ArgumentNullException("wav
If wavStream.CanRead Then
Dim StreamLength As Integer = Convert.ToInt32(wavStream.
'bring stream into a byte array
Dim byteStream(StreamLength) As Byte
wavStream.Read(byteStream,
'play the resource
Return PlaySound(byteStream, IntPtr.Zero, SND_ASYNC Or SND_MEMORY)
End If
End Function
End Class
ASKER
What do you guys thinking of playing mp3 & mid files?
Specially i am interesting in mid.
Specially i am interesting in mid.
For playing MIDI files , try using MCI functions .
Check these links ,
http://support.microsoft.com/default.aspx?scid=kb;en-us;171980
http://www.desertware.com/vbuniverse/playmidi.html
http://www.devx.com/vb2themax/Tip/18384
- Rajan
Check these links ,
http://support.microsoft.com/default.aspx?scid=kb;en-us;171980
http://www.desertware.com/vbuniverse/playmidi.html
http://www.devx.com/vb2themax/Tip/18384
- Rajan
for mp3 files, there is several solution :
- Use the Windows media player ocx file to lay music
- Use Managed DirectX 9 and the AudioVideoPlayback library to play mp3
directX is very simple to use but will have several drawbacks
- any client station will have to install the Managed DirectX 9 redist (about 35Mo)
- you will habe to include several directX DLL in your msi file --> the msi size will be increased by ~2Mo
- Use the Windows media player ocx file to lay music
- Use Managed DirectX 9 and the AudioVideoPlayback library to play mp3
directX is very simple to use but will have several drawbacks
- any client station will have to install the Managed DirectX 9 redist (about 35Mo)
- you will habe to include several directX DLL in your msi file --> the msi size will be increased by ~2Mo
ASKER
And last questing, is that possible to build music file in my app. I dont care if its gonna be larger.. I am not going to make instalation for this program and i just want it to be one exe with music inside of it, Is it possible to get it done.
For example i was it on some keygens.
For example i was it on some keygens.
ASKER
I mean for example i saw it on some keygens.
Properties of the file, set to Embedded resource then use .GetManifestResourceNames and .GetManifestResourceStream to read it back
maybe using managed directx?... check out Microsoft.DirectX.AudioVid
Esteban Felipe
www.estebanf.com