Solved

VB6: Loop sound files

Posted on 1998-12-23
13
1,778 Views
Last Modified: 2008-03-03
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
0
Comment
Question by:lankford
  • 6
  • 5
  • 2
13 Comments
 
LVL 3

Expert Comment

by:Sendoh
ID: 1452235
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.
0
 
LVL 2

Expert Comment

by:trillo
ID: 1452236
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
0
 

Author Comment

by:lankford
ID: 1452237
Trillo,

Speak on my friend . . .

Lankford
0
 
LVL 2

Expert Comment

by:trillo
ID: 1452238
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
0
 

Author Comment

by:lankford
ID: 1452239
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
0
 

Author Comment

by:lankford
ID: 1452240
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
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Accepted Solution

by:
Sendoh earned 100 total points
ID: 1452241
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
0
 
LVL 3

Expert Comment

by:Sendoh
ID: 1452242
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 !! ^_^
0
 

Author Comment

by:lankford
ID: 1452243
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

0
 
LVL 3

Expert Comment

by:Sendoh
ID: 1452244
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.
0
 
LVL 3

Expert Comment

by:Sendoh
ID: 1452245
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. ^_^
0
 

Author Comment

by:lankford
ID: 1452246
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
0
 

Author Comment

by:lankford
ID: 1452247
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!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

13 Experts available now in Live!

Get 1:1 Help Now