How to play two or more audio files one after another without freezing the form?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nicebasic
    New Member
    • Aug 2010
    • 91

    How to play two or more audio files one after another without freezing the form?

    I have written a small program in which playing two or more files one after another is needed.


    This is the Declaration in my BAS Module:
    Code:
    Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" _
    (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

    And this is the code I use to play the Audio Files:
    Code:
    sndPlaySound32 FileName, 0
    It can play files one after another, but the problem is that it causes the form to hang or freeze while the file is being played.

    How can I let the program to play files in order, but I can stop playing the Audio Files anytime I wish?

    Thank you in advance.
  • nicebasic
    New Member
    • Aug 2010
    • 91

    #2
    Let me be more clear. I wish to run some commands successively. For example, if you run the following code, you can't see the value of Label1 in Line 1 of the code, because the program runs the next lines and changes its value immediately before the audio file finishes playing:
    Code:
    Label1.caption = "Before"
    sndPlaySound32 FileName, 1
    Label1.caption = "After"
    Now look at this code:
    Code:
    Label1.caption = "Before"
    sndPlaySound32 FileName, 0
    Label1.caption = "After"
    It runs with no problem. But, if the Audio File is long and you wish to cancel its playing, the Form somehow seems to Hang or Freeze.

    I have even used DoEvents to solve the problem, but I failed. The label will be updated as desired, but you can't select the controls on the form. The mouse icon changes to hourglass which means the application is busy. I need to have a command button that can cancel playing the audio file, even if it has been forced to play.
    Code:
    Label1.caption = "Before"
    DoEvents
    sndPlaySound32 FileName, 0
    DoEvents
    Label1.caption = "After"

    I hope someone can help me find the solution.

    Comment

    • BigPapaN0z
      New Member
      • Dec 2011
      • 24

      #3
      Honestly, the built in audio in VB6 and Windows/WMP just suck. If you're wanting to use audio functions that are nonblocking (ie - won't freeze your program while they are playing) I would HIGHLY recommend finding a copy of FMOD 3.7x. Unfortunately, they don't offer this as a download from their website anymore. I'll look around on some backup discs and see if I can find you one I downloaded a while back.

      EDIT: Found it: FMOD 3.75
      Last edited by BigPapaN0z; Dec 20 '11, 02:27 AM. Reason: Link

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Just a note - DoEvents is used to interrupt your VB code briefly, to allow Windows a chance to do other things. An example might be doing a really long calculation which involves a lot of looping. It's unlikely to have much effect in a case like this, where your code doesn't have control.

        Comment

        • nicebasic
          New Member
          • Aug 2010
          • 91

          #5
          Thank you, BigPapaN0z.

          I tested your attached library. It's much better than sndPlaySound32, but I've abandoned this idea for now.

          I'm trying to find another way to accomplish this.

          Again, thank you so much for your help.

          Comment

          • Guido Geurs
            Recognized Expert Contributor
            • Oct 2009
            • 767

            #6
            This is a tool with MMControl to pick sounds and save them in an other folder.
            You can:
            - select a sound
            - start a sound at x seconds to jump over the intro (select delay in combobox)
            - stop playing
            - jump to next sound
            - put the filename in a collect list.
            - save the collected files in an other folder.
            Attached Files

            Comment

            • Rodney Roe
              New Member
              • Oct 2010
              • 61

              #7
              Have you tried using the mcisendstring API? I built a music player with this API. you can play sounds one at a time or even on top of each other at the same time, and i havn't ever had a crash from it.

              Comment

              • nicebasic
                New Member
                • Aug 2010
                • 91

                #8
                Thank you, Guido Geurs

                Originally posted by Guido Geurs
                This is a tool with MMControl to pick sounds and save them in an other folder.
                You can:
                - select a sound
                - start a sound at x seconds to jump over the intro (select delay in combobox)
                - stop playing
                - jump to next sound
                - put the filename in a collect list.
                - save the collected files in an other folder.

                Thank you very much, Guido Geurs,

                I have to admit that you're a great programmer. You have already answered one of my other questions regarding a "Borderless Form" and I'm really grateful to you.

                Thank you again for your support and your great attention.

                You're the best.

                Best wishes.

                Comment

                • nicebasic
                  New Member
                  • Aug 2010
                  • 91

                  #9
                  Dear Rodney Roe,

                  I haven't been able to use mciSending API. I find it rather difficult and complicated, because it's formless and every action should be coded there.

                  Moreover, it's not like MediaPlayer Object or other tools that can easily be embedded and configured.

                  If you have written a Music Player using mciSending API, I can start a new question and you can answer it as you wish.

                  Please let me know what to do.

                  This idea was very difficult for me. I was trying to write a Multiple-Choice Quiz that can play an Audio File, wait for a second or even less than a second, show the question, and wait for the User to pick one of the choices.

                  But for now, due to the difficulty of this kind of Audio Playing, I have abandoned the idea. I'm not disappointed. I need to study more.

                  Thank you very much for your attention to this question.

                  Comment

                  • Rodney Roe
                    New Member
                    • Oct 2010
                    • 61

                    #10
                    have you tried using this piece of code for your pause
                    Code:
                    Application.Wait Now + TimeValue("00:00:03")
                    It has issues some times but for the most part it has worked for me.

                    Also here is a class module that I created for mcisendstring that simplifies this API a lot.
                    all you have to do is "dim "some variable" as new cMusicPlayer" and the rest shouldn't be too hard to figure out. Disclaimer: This class module isn't perfect.

                    here's a piece of example code on how to set it up.
                    Code:
                    Dim mySong As New cMusicPlayer
                    Sub Play()
                      mySong.StopSong = False
                      mySong.Play = True
                      mySong.FileToPlay = "C:\Rodney\MyMusic\3 Doors Down\Away from the Sun\01 When I'm Gone.wma"
                    End Sub
                    Sub stopPlay()
                      mySong.StopSong = True
                    End Sub
                    Sub pauseSong()
                      mySong.Pause = True
                    End Sub
                    Sub ResumeSong()
                      mySong.ResumeSong = True
                    End Sub
                    Attached Files

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Code:
                      Application.Wait ...
                      What version of VB or VBA is this? VB6 does not have an Application object.

                      Comment

                      Working...