Results 1 to 37 of 37

Thread: How can I listen to a sound/music file in VB?

Hybrid View

  1. #1
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: How can I listen to a sound/music file in VB?

    Quote Originally Posted by Margaret
    Hi, thanks. I am getting the following error.

    Margaret.
    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.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: How can I listen to a sound/music file in VB?

    Quote Originally Posted by schoolbusdriver
    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.
    Attached Images Attached Images  
    Last edited by Margaret; Nov 8th, 2006 at 11:15 AM.

  3. #3
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: How can I listen to a sound/music file in VB?

    That definitely shouldn't happen. Can you zip up your project, including the res file, and attach it to a post please ?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: How can I listen to a sound/music file in VB?

    Quote Originally Posted by schoolbusdriver
    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
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by Margaret; Nov 9th, 2006 at 02:54 AM.

  5. #5
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: How can I listen to a sound/music file in VB?

    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:
    1. Private Sub cmdPlaySound_Click() 'PlaySound. Resource file. SYNCHRONOUS ONLY.
    2.  
    3.   Dim bytResFile() As Byte
    4.    
    5.    bytResFile = LoadResData(101, "SOUND")
    6.    Call PlaySoundResMem(bytResFile(0), App.hInstance, SND_MEMORY Or SND_SYNC)
    7.  
    8. 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.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    287

    Re: How can I listen to a sound/music file in VB?

    Quote Originally Posted by schoolbusdriver
    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:
    1. Private Sub cmdPlaySound_Click() 'PlaySound. Resource file. SYNCHRONOUS ONLY.
    2.  
    3.   Dim bytResFile() As Byte
    4.    
    5.    bytResFile = LoadResData(101, "SOUND")
    6.    Call PlaySoundResMem(bytResFile(0), App.hInstance, SND_MEMORY Or SND_SYNC)
    7.  
    8. 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.

    Margaret.

  7. #7
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: How can I listen to a sound/music file in VB?

    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.

  8. #8
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: How can I listen to a sound/music file in VB?

    Quote Originally Posted by Margaret
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width