naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.38k stars 1.09k forks source link

Even if waveout.Play runs in its own thread, Windows events pause the audio #1083

Open vijaysridhara opened 8 months ago

vijaysridhara commented 8 months ago

I am writing a Visual Novel Game Engine, where the audio loops throughout the game (No DirectX, just Winforms). Every time an anination sequence or a complex paint event happens on the form, the audio pauses, and resumes after the event If I just hold mouse down on close and minimize buttons of the form also, the audio pauses, and resumes after I release. This should ideally have no dependency on the Playing thread, as it is an independent thread looping through audio ...

Private Sub PlayAudio()
        wvOut.Volume = vol
        Dim prevVol As Single = vol
LoopIt:
        wvOut.Play()
        While wvOut.PlaybackState = PlaybackState.Playing
            Threading.Thread.Sleep(100)
            If audMemStr Is Nothing Then Exit Sub
            If blkA Is Nothing Then Exit Sub
            If wvOut Is Nothing Then Exit Sub
            If prevVol <> vol Then
                wvOut.Volume = vol
                prevVol = vol
            End If
            If stopAdudio Then
                wvOut.Stop()
            End If

        End While
        If wvOut.PlaybackState = PlaybackState.Stopped Then
            If stopAdudio Then Exit Sub
            If audMemStr Is Nothing Then Exit Sub
            If blkA Is Nothing Then Exit Sub
            If wvOut Is Nothing Then Exit Sub
            blkA.Seek(0, SeekOrigin.Begin)
            If stopAdudio = False Then
                GoTo LoopIt
            End If
        End If

    End Sub
markheath commented 7 months ago

I'd recommend getting rid of the loop and Threading.Thread.Sleep(100), and instead subscribe to the PlaybackStopped event of wvOut