Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Embedding Music into your VB 6 Program 3

Status
Not open for further replies.

andrewvampire

Programmer
Feb 13, 2001
18
US
I need to know how to embed music into my program so at startup it will play. I also would like to know how to put OLE'S in a file menu.

Thanks,
J.Conway

P.S: Whats is API (I'm pretty much a beginner at VB)
 

Put this declaration in a Module:

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


Put the following code anywhere in your application:

Dim lngRC As Long
lngRC = PlaySound("c:\myfile.wav", 0&, 0&) Tarek
 
Pretty much this is helpful except there are a few bugs in the code in it that I don't know how to fix.

First off when you hit play the form won't load up intil the song is done playing.

Second I want the song to keep playing
And third, is there any possible way of shorting the load time for larger music files?

 
The last number in the bracket determines how it will play. See the Microsoft internet help page on APIs for explanation. I 1 allows you to interrupt the music with a new one. I have heard that it is a good idea to call the function with a "" as a filename before you start another tune otherwise you can get a memory leak.
I use in my Module1:
Public Declare Function sndPlaySound Lib "winMM.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
The function to call to start playing is
sub PlayIt_Click()
Title="the music.wav"
X = sndPlaySound("" & Title, 1)
doevents
X = sndPlaySound("c:\sounds\" & Title, 1)
end sub
It starts straight away on a wave file of 5 mins long
 
Regarding the form not displaying until the song is done play, I would have to guess that you are calling the API in the FORM_LOAD routine. This routine gets called while the form is loading, and the form itself will not be displayed until after all code in there has executed. If you API call is in the FORM_LOAD, then try moving it to the FORM_ACTIVATE. This will cause the API to get called when the form is visible and has focus. One word of warning however, the code in the FORM_ACTIVATE gets called whenever the form receives focus, so if you have multiple forms in you appliction, and you switch to a different form and then come back to the first form, the FORM_ACTIVATE will be called again.
 
Using form activate still doesn't work like I want it to, I want the music to be playing while the user is useing the program, with those codes it doesn't allow the user to do anything while the music is playing.

Is there anyway I can fix this problem??
 
Tedsmith's code should have worked fine. You might try the alternate declaration (remember that all API decs are case sensitive): [tt]
Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long [/tt]


The last parameter, dwFlags, is the one you might be interested in. Here are the possible values:

[tt][tab]SND_ALIAS = &H10000[/tt]
lpszName is a string identifying the name of the system event sound to play.
[tt][tab]SND_ALIAS_ID = &H110000[/tt]
lpszName is a string identifying the name of the predefined sound identifier to play.
[tt][tab]SND_APPLICATION = &H80[/tt]
lpszName is a string identifying the application-specific event association sound to play.
[tt][tab]SND_ASYNC = &H1[/tt]
Play the sound asynchronously -- return immediately after beginning to play the sound and have it play in the background.
[tt][tab]SND_FILENAME = &H20000[/tt]
lpszName is a string identifying the filename of the .wav file to play.
[tt][tab]SND_LOOP = &H8[/tt]
Continue looping the sound until this function is called again ordering the looped playback to stop. SND_ASYNC must also be specified.
[tt][tab]SND_MEMORY = &H4[/tt]
lpszName is a numeric pointer refering to the memory address of the image of the waveform sound loaded into RAM.
[tt][tab]SND_NODEFAULT = &H2[/tt]
If the specified sound cannot be found, terminate the function with failure instead of playing the SystemDefault sound. If this flag is not specified, the SystemDefault sound will play if the specified sound cannot be located and the function will return with success.
[tt][tab]SND_NOSTOP = &H10[/tt]
If a sound is already playing, do not prematurely stop that sound from playing and instead return with failure. If this flag is not specified, the playing sound will be terminated and the sound specified by the function will play instead.
[tt][tab]SND_NOWAIT = &H2000[/tt]
If a sound is already playing, do not wait for the currently playing sound to stop and instead return with failure.
[tt][tab]SND_PURGE = &H40[/tt]
Stop playback of any waveform sound. lpszName must be an empty string.
[tt][tab]SND_RESOURCE = &H4004[/tt]
lpszName is the numeric resource identifier of the sound stored in an application. hModule must be specified as that application's module handle.
[tt][tab]SND_SYNC = &H0[/tt]
Play the sound synchronously -- do not return until the sound has finished playing.

Try this:
[tt]
Dim retval As Long 'this will be non-zero if successful
retval = PlaySound("C:\Windows\MySound.wav", 0, SND_FILENAME Or SND_ASYNC Or SND_NODEFAULT Or SND_LOOP)
If retval = 0 Then
MsgBox "Couldn't play that sound!, vbCritical, "Oops!"
End If
[/tt]


Hopefully, you have a file in your Windows folder called "MySound.wav". You will need to include the following line in the Form Unload event to prevent the memory leakage Tedsmith mentioned:
[tt]
retval = PlaySound("", 0, SND_PURGE Or SND_NODEFAULT)
[/tt]

VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top