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 the
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
... And here's some nice artciles on how to use the Speech SDK
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.
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?
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
Yes but she'll need to use different code to do that won't she schoolbusdriver?
Yes
@ Keith, how about this. Plays from memory BEFORE (and after) compilation. Problems ? - it won't play asynchronously. Crashes if you try to use SND_ASYNC
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
@ Keith, how about this. Plays from memory BEFORE (and after) compilation. Problems ? - it won't play asynchronously. Crashes if you try to use SND_ASYNC
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
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
Of course this crashes when using the SND_ASYNC flag,
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.
Of course this crashes when using the SND_ASYNC flag,
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
Last edited by schoolbusdriver; Nov 8th, 2006 at 08:38 AM.
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.
EDIT:You may find post 19 in THIS THREAD useful. (The whole thread shows a few different ways of using the playsound API)
Last edited by schoolbusdriver; Nov 8th, 2006 at 08:25 AM.
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.
I cant find any resource file to add in my project. What to do now?
Note: I got it. And created a resource file and put a .wav file. Now the program hangs.
Margaret.
Last edited by Margaret; Nov 8th, 2006 at 11:15 AM.
That definitely shouldn't happen. Can you zip up your project, including the res file, and attach it to a post please ?
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
Last edited by Margaret; Nov 9th, 2006 at 02:54 AM.
Hi Margaret, no probs. The pic and code was enough 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:
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
("WAVE" also works)
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 If you want this alternative, just let me know.
Hi Margaret, no probs. The pic and code was enough 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:
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
("WAVE" also works)
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.
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.
Last edited by schoolbusdriver; Nov 13th, 2006 at 03:03 PM.
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.
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.
schoolbusdriver gave me different code to play wave files and I had to compile before I could hear a sound.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
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.
schoolbusdriver gave me different code to play wave files and I had to compile before I could hear a sound.
Hi,
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.
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.
Call PlaySound(SoundBuffer, App.hInstance, SND_RESOURCE Or SND_ASYNC)
End Sub
Private Sub cmdSong1_Click()
Call PlaySounds(101)
End Sub
Are your sound types "SOUND"?
Last edited by Keithuk; Nov 11th, 2006 at 06:07 PM.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
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.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
You need to add the API declaration and constants to the top of the form frmQuiz1's code - just under "Option Explicit"
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........
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.
Did you try the project in the zip file "Sounds.zip" I attached to post 23 ? Did it work OK ?
But coud not get the sound. I can hear just a sound like some error beep sound.
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.
Does your computer play sounds anyway through Windows Media Player?
You can check if you comp can play anything with the waveOutGetNumDevs API.
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
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
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...
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.
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 ?
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,
No, everytime I play either there is just a beep sound or system hangs.
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.