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?

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

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

  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?

    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.

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

  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?

    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.

  6. #6
    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.

  7. #7

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

    Margaret.

  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?

    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.
    VB Code:
    1. Const SND_ASYNC = &H1         'Play asynchronously.
    2. Const SND_PURGE = &H40        'Release memory.
    3. Const SND_RESOURCE = &H40004  'Name is a resource name or atom.
    4.  
    5. Private Declare Function PlaySound Lib "winmm.dll" Alias _
    6. "PlaySoundA" (ByVal lpszName As Long, ByVal hModule As Long,_
    7. ByVal dwFlags As Long) As Long
    8.  
    9. Private Sub PlaySounds(SoundNum As Long)
    10.  
    11. Dim SoundBuffer As String
    12.    
    13. SoundBuffer = StrConv(LoadResData(SoundNum, "SOUND"), vbUnicode)
    14. 'Reference nothing and purge memory.
    15. Call PlaySound(0, App.hInstance, SND_PURGE)
    16. 'Reference the res file.
    17. Call PlaySound(SoundBuffer, App.hInstance, SND_RESOURCE Or SND_ASYNC)
    18.  
    19. End Sub
    20.  
    21. Private Sub cmdSong1_Click()
    22.  
    23. Call PlaySounds(101)
    24.  
    25. 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.

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