Dear Friends,
How can I listen/open a sound/music file in VB6 by pressing a cmdPLAY button?
Thanks in advance.
Regards,
Margaret.
Printable View
Dear Friends,
How can I listen/open a sound/music file in VB6 by pressing a cmdPLAY button?
Thanks in advance.
Regards,
Margaret.
One option is to use the MCI control
Hi,Quote:
Originally Posted by HanneSThEGreaT
Actually what I need is, I would like to record question and answers in .wav files using Windows Sound Recorder. Then, when I open my quiz program and click on play button thefiles should start using windows media player.VB Code:
.wav
How can I do this? What is the code?
Regards,
Margaret.
If you want to convert text in a TextBox to a wave file then you need to download Speech SDK 5.1 ;)
Speech SDK 5.1
... And here's some nice artciles on how to use the Speech SDK ;)
http://www.codeguru.com/vb/gen/vb_mi...le.php/c11019/
http://www.codeguru.com/vb/gen/vb_mi...le.php/c11085/
http://www.codeguru.com/vb/gen/vb_mi...le.php/c11223/
http://www.codeguru.com/vb/gen/vb_mi...cle.php/c11381
I already have some .wav audio files which I recorded using Microsoft Sound Recorder. Just, I need to use them in VB application. I think I need to specify the path of that file and write code to execute the Windows Media Player to play that audio file.Quote:
Originally Posted by HanneSThEGreaT
How to do it?
Margaret.
The ball game keeps changing... :)
have a look at this link :
http://www.thescarms.com/vbasic/volumecd.asp
I sincerely hope it helps you now Maragaret :)
VB Code:
Private Declare Function PlaySound& Lib "winmm.dll" _ (ByVal lpszName As String, ByVal hModule As Long, _ ByVal dwFlags As Long) Const SND_ASYNC = &H1 Const MyFile = "c:/WINDOWS/MEDIA/logoff.wav" Private Sub cmdPlay_Click() Call PlaySound(MyFile, 0&, SND_ASYNC) End Sub
:lol:Quote:
Originally Posted by HanneSThEGreaT
And there's more. If you want to compile the sounds into your app, you can put them in a resource file and play them from there ;)
Yes but she'll need to use different code to do that won't she schoolbusdriver? :bigyello:Quote:
Originally Posted by schoolbusdriver
Yep. Even MP3/WMA:Quote:
And there's more. If you want to compile the sounds into your app, you can put them in a resource file and play them from there
http://pscode.com/vb/scripts/ShowCod...67020&lngWId=1
Hi,Quote:
Originally Posted by Hassan Basri
Where should I write the above code on form?
Margaret.
Yes :lol:Quote:
Originally Posted by Keithuk
@ Keith, how about this. Plays from memory BEFORE (and after) compilation. Problems ? - it won't play asynchronously. :rolleyes: Crashes if you try to use SND_ASYNC :cry:
VB Code:
Option Explicit 'Pass an ANY Resource reference to lpszName. NOTE:- "ByRef" Private Declare Function PlaySoundResMem Lib "winmm.dll" Alias "PlaySoundA" _ (ByRef lpszName As Any, ByVal hModule As Long, ByVal dwFlags As Long) As Long Const SND_MEMORY = &H4 'Use a byte array. Const SND_SYNC = &H0 'Play synchronously (default) Private Sub Command1_Click() 'PlaySound. Resource file. SYNCHRONOUS ONLY. Dim bytResFile() As Byte bytResFile = LoadResData(101, "SOUND") Call PlaySoundResMem(bytResFile(0), App.hInstance, SND_MEMORY Or SND_SYNC) End Sub
Hi, thanks. I am getting the following error.
Margaret.
Of course this crashes when using the SND_ASYNC flag,Quote:
Originally Posted by schoolbusdriver
bytResFile must be declared global.
PlaySound will return immediately, the method will be left,
and bytResFile will fall out of scope, therefore be freed by the VB Runtime,
the memory isn't accessible anymore, PlaySound tries to access an
invalid memory address, Crash.
It DOES work! - thanks. Consider my above code suitably ammended :bigyello:Quote:
Originally Posted by Arne Elster
You need to add a resource file to your project, and put a .wav file into the resource file. Look in my signature for "Resource file tutorial" - it's pretty good.Quote:
Originally Posted by Margaret
EDIT:You may find post 19 in THIS THREAD useful. (The whole thread shows a few different ways of using the playsound API)
I cant find any resource file to add in my project. What to do now?Quote:
Originally Posted by schoolbusdriver
Note: I got it. And created a resource file and put a .wav file. Now the program hangs.
Margaret.
That definitely shouldn't happen. Can you zip up your project, including the res file, and attach it to a post please ?
Quote:
Originally Posted by schoolbusdriver
Hi,
Thanks a lot for your usual help. I have attached the project. But could not attach the Resourse file & Add Questions form due to the limitation of (size is too large to be attached : zip file is 868 KB) attachment.
Actually what I need for this project is, I have questions and options to select correct answer. All are in text format. Now, I would like to record each question alongwith its options in an audio file giving a name like..Question1.wav, Question2.wav, Question3.wav, ........Question50.wav (.wav or .mp3 file format).
So, when users start the quiz they can see the question & its options in text and when press button "Play Sound" they can listen to that question & options.
How can I do this? Hope to get usual help from you.
Regarding resource file path I have attched .jpg file.
Regards,
Margaret
Hi Margaret, no probs. The pic and code was enough :thumb: You need to change the "Type" of resource to "SOUND". In the resource editor, double-click on the "101" and you'll get an "Edit Properties" dialog. Just change the Type to "SOUND". The play code needs to be changed to suit:("WAVE" also works)VB Code:
Private Sub cmdPlaySound_Click() 'PlaySound. Resource file. SYNCHRONOUS ONLY. Dim bytResFile() As Byte bytResFile = LoadResData(101, "SOUND") Call PlaySoundResMem(bytResFile(0), App.hInstance, SND_MEMORY Or SND_SYNC) End Sub
BTW, this code will cause the app to "freeze" while the sounds are played - which is what I assume you want for a Quiz program ie - stop user interaction until the questions/answers are played. As Arne Elster pointed out above, a minor change can alter this behaviour - the only problem being that the "sound file" stored in "bytResFile()" will remain in memory for the duration of the app. Although it can be erased, there is no easy (one liner) way to do this automatically - although it is possible. A better way is to use code that doesn't involve this overhead - the problem being it will only play sounds after you've made an exe. It won't play in the IDE :rolleyes: If you want this alternative, just let me know.
The audio file name is "Welcome.wav". I put this file in Resource Folder. I am still confused since it doesn't play the sound.Quote:
Originally Posted by schoolbusdriver
Margaret.
Try the project in this zip file. Very, very rarely, PlaySound fails on some PCs. Let me know if it works OK.
EDIT: Attachment removed. See later post.
Providing you have Type right = "SOUND" and the ID is a number you may have problems hearing the sound in IDE. Compile the exe and try it again.Quote:
Originally Posted by Margaret
schoolbusdriver gave me different code to play wave files and I had to compile before I could hear a sound. ;)
Hi,Quote:
Originally Posted by Keithuk
Still, I am confused. Suppose I have 10 audio files in a folder "Audio Songs" say, Song1, Song2, Song3 ......Song10, and the folder is in the same project folder. How can I listen to a song Song1 when I click on cmdSong1.
Kindly let me know the easiest way to do this, please.
Margaret.
When you add these songs to a resource file I normally use the filename without the extension. Because that is easy to play that sound. But if you are using schoolbusdriver code with PlaySound API you need to change this to a number so write them down.
Song1 = 101
Song2 = 102
Song3 = 103
Song4 = 104 etc
Without seeing the code you are using I can only guess at how you are playing these sounds.
Are your sound types "SOUND"?VB Code:
Const SND_ASYNC = &H1 'Play asynchronously. Const SND_PURGE = &H40 'Release memory. Const SND_RESOURCE = &H40004 'Name is a resource name or atom. Private Declare Function PlaySound Lib "winmm.dll" Alias _ "PlaySoundA" (ByVal lpszName As Long, ByVal hModule As Long,_ ByVal dwFlags As Long) As Long Private Sub PlaySounds(SoundNum As Long) Dim SoundBuffer As String SoundBuffer = StrConv(LoadResData(SoundNum, "SOUND"), vbUnicode) 'Reference nothing and purge memory. Call PlaySound(0, App.hInstance, SND_PURGE) 'Reference the res file. Call PlaySound(SoundBuffer, App.hInstance, SND_RESOURCE Or SND_ASYNC) End Sub Private Sub cmdSong1_Click() Call PlaySounds(101) End Sub
Hi,Quote:
Originally Posted by Keithuk
I have attached the jpg file to show how I have done the coding. Though there are no errors, I cound not hear any audio.
Hope to get help from you.
Margaret.
Well have I said before I can't hear any sounds using PlaySound API on my comp you have to compile the exe first then run the exe. ;)
Hi,Quote:
Originally Posted by Keithuk
I am getting the following attached error while compiling the exe.
Margaret.
You need to add the API declaration and constants to the top of the form frmQuiz1's code - just under "Option Explicit"
As KeithUK pointed out, this doesn't work on his PC, but works on others. There are at least 5 different ways of calling PlaySound, some of which may not work unless the program is compiled, and others may not work on YOUR PC, but work on other PCs.Code:Option Explicit
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _
(ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Const SND_ASYNC = &H1 'Play asynchronously.
Const SND_PURGE = &H40 'Release memory.
Const SND_RESOURCE = &H40004 'Name is a resource name or atom.
Dim Marks As Double
Private Sub Form_Load()
'etc, etc........
Did you try the project in the zip file "Sounds.zip" I attached to post 23 ? Did it work OK ?
Hi, it is nice to see you online. I tried both. But coud not get the sound. I can hear just a sound like some error beep sound.Quote:
Originally Posted by schoolbusdriver
Margaret.
I'll say this a third time Margaret. I can't hear any sounds when I use PlaySound API you have to compile the exe first. ;)Quote:
Originally Posted by Margaret
Does your computer play sounds anyway through Windows Media Player?
You can check if you comp can play anything with the waveOutGetNumDevs API. :thumb:
VB Code:
Private Declare Function waveOutGetNumDevs Lib "winmm.dll" () As Long Private Sub Form_Load() Dim rtn As Long rtn = waveOutGetNumDevs() If rtn = 0 Then MsgBox "Your system cannot play Sound Files.", 16, "SoundCard Check" End If
I've had this problem before with playing sounds from resource files. I would hear a beep until I compiled, ran the EXE, and then ran the project again from the IDE.
But I'm guessing you probably already tried this...
Edit: Just saw Keith's post. ;)
Yes, I am also having the same problem. My computer plays sounds very well. So, the sound card is ok. Any other possible way to play audio files which are stored in my project folder.Quote:
Originally Posted by DigiRev
Hope to get some help.
Margaret.
I've put together a little project that uses several variations of the PlaySound APIs. Unzip both files into the same folder and run the project in the IDE, then try compiling it and running the executable. Do any of them work, and if so, which ?
Hi,Quote:
Originally Posted by schoolbusdriver
No, everytime I play either there is just a beep sound or system hangs.
Regards,
Margaret.
Well, that answers one question - it looks like your PC has similar problems as DigiRev's and Keithuk's when trying to use the PlaySound APIs. I haven't got any idea what causes it. There are the alternatives such as those mentioned in earlier posts. Also, in my signature - VB - mciSendString - audio/video playback / video splashscreen etc from resource file. Although this is mainly intended as a splashscreen, it can easily be adapted to simply play sounds from a resource file - the form doesn't have to be visible.