CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    May 2008
    Posts
    1

    Play Background Music in Sequence

    Well I've got five audio files that I want to play in the background of my program one after the other (like a playlist), however, I can only get one to play (which is the last one). I have also tried changing AudioPlayMode.Background to AudioPlayMode.WaitToComplete but with no luck. The music plays correctly but the form does not load. I am trying to find a way to get all five songs to play one after the other in the background of my program.

    Here is the code:

    Code:
        Private Sub frmStart_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Music(4) As Object
            Music(0) = "Music1.wav"
            Music(1) = "Music2.wav"
            Music(2) = "Music3.wav"
            Music(3) = "Music4.wav"
            Music(4) = "Music5.wav"
            Dim MusicCount As New Integer
            For MusicCount = 0 To 4
                My.Computer.Audio.Play(Music(MusicCount), AudioPlayMode.Background)
            Next
        End Sub
    It would be great if you could use simple terms as I'm a novice when it comes to programming. All help would be greatly appreciated, thanks.

  2. #2
    Join Date
    Jun 2007
    Posts
    16

    Re: Play Background Music in Sequence

    This is because the for loop will not start playing the next until the first is finished. If you want to perform multiple operations at once that take a long time you will need to use separate threads.

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Play Background Music in Sequence

    It doesn't play the first, it plays the last, because it doesn't wait until the first finishes, before starting the next. It plays the last one until it's done.

    http://allapi.mentalis.org/apilist/PlaySound.shtml

    You can use the API, but I'd bet that there is a way to use the ASYNC flag to have it wait until it finishes before going to the next song.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Play Background Music in Sequence

    Quote Originally Posted by dglienna
    It doesn't play the first, it plays the last, because it doesn't wait until the first finishes, before starting the next. It plays the last one until it's done.

    http://allapi.mentalis.org/apilist/PlaySound.shtml

    You can use the API, but I'd bet that there is a way to use the ASYNC flag to have it wait until it finishes before going to the next song.
    Spot on David. You can make PlaySound wait for the first to finish before the second starts.

    And even loop..

    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Play Background Music in Sequence

    Hmmm oops.. Just noticed that to do that the API does not return control to the app...

    Will have to look at another option...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Play Background Music in Sequence

    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Play Background Music in Sequence

    Wow, that really is a bug.
    I tried threading it, and it plays about 2-3 seconds of the first song, and then jumps to the last song, in spite of the inellisense definition.
    I could understand it happening in the load event of the form, but not in a thread.

    If you can't figure it out with the links posted then here is a solution using threads.
    EDIT: Full solution at post 10.

    Code:
        Dim t As New Threading.Thread(AddressOf PlayIt)
        Dim t2 As New Threading.Thread(AddressOf PlayIt2)
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            t.Start()
            t2.Start()
        End Sub
        Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            t.Abort()
            t2.Abort()
        End Sub
        Private Sub PlayIt()
            My.Computer.Audio.Play("C:\Blind.wav", AudioPlayMode.Background)
        End Sub
        Private Sub PlayIt2()
            Threading.Thread.Sleep(120002)
            My.Computer.Audio.Play("C:\Adidas.wav", AudioPlayMode.Background)
        End Sub
    Basically the second thread waits 2 minutes and 2 seconds for the first song to finish.
    It's a hack but hey it's easy to understand, and won't tie up the user interface. Add more sub like PlayIt3, ...

    Good luck
    Last edited by TT(n); February 13th, 2009 at 06:48 PM.

  8. #8
    Join Date
    May 2004
    Location
    Osijek
    Posts
    61

    Re: Play Background Music in Sequence

    Quote Originally Posted by MonaroMan View Post
    Well I've got five audio files that I want to play in the background of my program one after the other (like a playlist), however, I can only get one to play (which is the last one). I have also tried changing AudioPlayMode.Background to AudioPlayMode.WaitToComplete but with no luck. The music plays correctly but the form does not load. I am trying to find a way to get all five songs to play one after the other in the background of my program.
    Use AutoPlayMode.WaitToComplete - that will ensure that each audio waits for previous to finish. Dump your existing code in BackgroundWorker_DoWork event. In Form_Load start BackgroundWorker. That should do the trick.

    P.S. Check CancelAsync to implement cancel (e.g. on form unload).

  9. #9
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Play Background Music in Sequence

    jmedved,
    No, it does the same thing.

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            BackgroundWorker1.RunWorkerAsync()
        End Sub
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            My.Computer.Audio.Play("C:\Blind.wav", AudioPlayMode.WaitToComplete)
            My.Computer.Audio.Play("C:\Adidas.wav", AudioPlayMode.WaitToComplete)
        End Sub

  10. #10
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Resolved

    dglienna makes the call!

    This took quite a few attempts to hone in on.
    There may be other ways, but this one works nicely here.

    Code:
        Private Const SND_ASYNC As Int32 = 1
        Private Const SND_NOSTOP As Int32 = 16
        Private Const SND_PURGE As Int32 = 64
        Private Const SND_FILENAME As Int32 = 131072
        Private Declare Function apiPlaySound Lib "winmm" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Int32, ByVal dwFlags As Int32) As Int32
        Dim t As New Threading.Thread(AddressOf PlaySync)
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            t.Start() 'Start thread
        End Sub
        Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            t.Abort() 'Stop thread
        End Sub
    
        Private Sub PlaySync() 'Play songs syncronously one after the other is finished
            WaitToComplete("C:\SoundFile1.wav")
            WaitToComplete("C:\SoundFile2.wav")
            WaitToComplete("C:\SoundFile3.wav")
        End Sub
        Private Sub WaitToComplete(ByVal sPath As String) 'Wait up to an hour
            For i As Int32 = 1 To 900
                If apiPlaySound(sPath, 0, SND_FILENAME Or SND_ASYNC Or SND_NOSTOP) <> 0 Then 'If started
                    If i > 1 Then  'If finished and now playing a second time
                        apiPlaySound(Nothing, 0, SND_FILENAME Or SND_PURGE) 'Stop song from playing twice
                        Exit For
                    End If
                End If
                Threading.Thread.Sleep(4000) 'sleep 4 seconds
            Next
        End Sub
    Last edited by TT(n); February 13th, 2009 at 06:51 PM. Reason: cleaned up and commented

  11. #11
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    RESOLVED bonus

    This is kinda cool too.
    If you purge the current song file playing, the entire WaitToComplete sub is aborted.
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            apiPlaySound(Nothing, 0, SND_FILENAME Or SND_PURGE) 'Aborts current WaitToComplete
        End Sub
    I didn't expect that bonus either.
    Last edited by TT(n); February 13th, 2009 at 07:16 PM.

  12. #12
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Play Background Music in Sequence

    Nice job, as usual! Catch 'ya soon!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured