Link to home
Start Free TrialLog in
Avatar of lankford
lankford

asked on

VB6: Loop sound files

Two parts.

1) Does anyone know of an easy way to loop short wav files in VB without hearing a slight pause between loops?  This is my current problem with the multimedia control.

2) Does anyone know of a place to pick up good wav files for looping (rain forest, ocean, brook, etc.)?

Lankford
Avatar of Sendoh
Sendoh
Flag of New Zealand image

Hi !
 I think the it's had to depends on what are the size of wav file are you playing and what are the target machine. If your wav is huge and you machine(ram) is low, no matter how well your programm, it'll effected.

Hope you'll get what you want.
Avatar of trillo
trillo

Hi.

The multimeda control is rather slow. A faster way is using MCI commands. If you know how to use them, just use the "repeat" keyword with the play command.
If you dont know anythin about MCI commands just tell me and I'll explain.

Trillo
Avatar of lankford

ASKER

Trillo,

Speak on my friend . . .

Lankford
Here it is... You should use the mciSendString function which allows you to play wav files, midi and avi (also other multimedia files)
First you have to make the API declaration on the form.
Private Declare Function mciSendString Lib "winMM.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hWndCallback As Integer) As Long

Next you open the file
Call mciSendString("open c:\somefile.wav type waveaudio alias myaudio", 0, 0, 0)

Next you play the file continuously
Call mciSendString("play myaudio from 1 repeat", 0, 0, 0)

When you want to stop
Call mciSendString("stop myaudio")

Finally you close the device
Call mciSendString("close myaudio")

That is all, as you can see it's very simple, and it is more elegant than other methods such as external OCX controls or embedded objects using OLE... It is also faster and you waste less memory.

The mciSendString function is called in this example with the "Call" keyword, although it's useful to call it with the "x = mciSendString (....)" syntax, because this function always returns 0 if succesfull. So you could test the "x" variable, and if it is nonzero, then some error occurred.
Note also that when opening the file, you selected an alias (which can be any name), you must use this alias for subsequent commands, it sort ot identifier for the device.
All operations in MCI (play, record, save to file, etc..), must be done between the open and close commands (these refer to opening an closing the device)
If you have more questions, just tell me.

Merry Christmas !
Trillo
Trillo,

Thank you for the answer.  I love it!  You are probably wondering why I gave you a failing answer.  The "repeat" command is giving me problems.  Whenever I include the command, I get error 259 - MCIERR_UNRECOGNIZED_KEYWORD.  If I remove the repeat keyword, then it works fine.

Do you know why my mcisendcommand function doesn't support the repeat keyword?

Thanks again,

Lankford
Read the following topic in the MSDN library:

\Platform SDK\Graphics and Multimedia Services\Multimedia Reference\Multimedia Command Strings\play

The first table on this topic seems to indicate that the play command for the wavaudio device does not support the repeat keyword.

Does anyone have any ideas.  Can I be notified when the operation reaches the end and tell it to play again from the beginning?  If I do this, will I get a gap in playback like I do with the VB control?

Lankford
ASKER CERTIFIED SOLUTION
Avatar of Sendoh
Sendoh
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi !
Although the "MCISendstring" are one of the solution. But I think the following code are more suitable for Wave player.

Const SND_ASYNC = &H1         '  play asynchronously
Const SND_LOOP = &H8
Const SND_NOSTOP = &H10
Const SND_SYNC = &H0         '  play synchronously (default)

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Private Sub Command4_Click()
    sndPlaySound "c:\temp\mci\0.wav", SND_ASYNC
End Sub

I'm not sure whether this will solve your "GAP" problem.
Anyway, it worth a try !! ^_^
I will give you the points.  Your answer works great.  But first, I have to ask.  My simple app below executes a system beep after clicking the stop button.  The sndplaysound function returns 1 (which is false which is an error right?).  After returning 1, the system beeps.  Do you know why?  After I hear back from you I will reward you the points.

Thanks for the answer!  [see code below]


Private Sub cmdPlay_Click()
    sndPlaySound "C:\work\looping audio\space.wav", SND_ASYNC Or SND_LOOP
End Sub

Private Sub cmdStop_Click()
    sndPlaySound Chr(0), SND_SYNC
End Sub

Hi !

I'd tried out this, but on my machine its doesn't launch a system beep and its return value is '0' instead of '1'.
I don't know what is the problem. May be you can check it out on your "sounds" icon in your control panel. Or
maybe you can tried
       sndPlaySound "",0
instead.
Oh ! Hi !

I've found the problem, in this case, if the system doesn't found the files (sndPlaySound ch(0),0 ), the default windows sound will played.
You can set it in your machine control panel under "Sounds" icon. You may take a tried on it by specifying others sound file and the click on "stop" button.

Hope this'll solve your problem. ^_^
Thanks for the response.  You are right -- the sndPlaySound function is triggering the play of the default sound as defined in the control panel.  

Is there any way to stop the sound without triggering this action?

Lankford
Here is how to avoid the system beep.

Const SND_NODEFAULT = &H2         '  silence not default, if sound not found

Private Sub cmdStop_Click()
    sndPlaySound Chr(0), SND_SYNC Or SND_NODEFAULT
End Sub

Thanks for the answer!