Solved

Background music in vb .net

Posted on 2004-03-23
10
1,938 Views
Last Modified: 2007-12-19
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?
0
Comment
Question by:g00r00
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 12

Expert Comment

by:esteban_felipe
ID: 10664705
Hi g00r00,

maybe using managed directx?... check out Microsoft.DirectX.AudioVideoPlayback  documentation

Esteban Felipe
www.estebanf.com
0
 
LVL 3

Accepted Solution

by:
pamboo earned 25 total points
ID: 10665312
Check this if it helps,

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

   PlaySound ("1.wav", 0, 1)
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 10667220
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\tada.wav")
    End Sub

    Private Sub PlayS(ByVal sound As String)
        Dim thread As New SoundThread()
        Dim mThread As New System.Threading.ThreadStart(AddressOf thread.newThread)
        Dim oThread As New System.Threading.Thread(mThread)
        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
0
 
LVL 4

Expert Comment

by:Jarodtweiss
ID: 10671332
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.InteropServices
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("wavStream")

    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
0
 

Author Comment

by:g00r00
ID: 10674220
What do you guys thinking of playing mp3 & mid files?
Specially i am interesting in mid.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 3

Expert Comment

by:pamboo
ID: 10674535
0
 
LVL 4

Expert Comment

by:Jarodtweiss
ID: 10675256
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
0
 

Author Comment

by:g00r00
ID: 10678062
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.
0
 

Author Comment

by:g00r00
ID: 10678074
I mean for example i saw it on some keygens.
0
 
LVL 4

Expert Comment

by:Nazdor
ID: 10747671
Properties of the file, set to Embedded resource then use .GetManifestResourceNames and .GetManifestResourceStream to read it back
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# GridRow get Old/New Value 1 55
Getfiles in vb.net 28 43
Icons and Colors for Terms 3 24
Not showing JavaScript in the list 5 38
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now