Bang1338
Bang1338

Reputation: 11

How to loop playback in NAudio? (VB.NET)

I tried to load mp3 from Resource and it success. But I want loop playback I search around and found nothing... Here's the code:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mp3 As MemoryStream = New MemoryStream(My.Resources.RequiemAtDusk)
        Dim read As Mp3FileReader = New Mp3FileReader(mp3)
        Dim waveOut As WaveOut = New WaveOut
        waveOut.Init(read)
        waveOut.Play()
    End Sub

Upvotes: 0

Views: 358

Answers (2)

Danny
Danny

Reputation: 1

This works for me:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

timer1.Interval = 1000
timer1.start

end sub

dim waveOut as WaveOut = new WaveOut

function soundtrack()


      
            Dim mp3file As MemoryStream = New MemoryStream(My.Resources.RequiemAtDusk) '' You can use your MP3 file here
            Dim reader As Mp3FileReader = New Mp3FileReader(mp3file)
   
     
            waveOut.Init([reader)
            waveOut.Play()
      
     End Function

Private Sub Timer1_Tick(sender as Object, e As EventArgs) Handless Timer1.Tick

if waveout.PlaybackState = 0 then
soundtrack()
end if

End Sub

Upvotes: 0

Bang1338
Bang1338

Reputation: 11

So I did it:

Public Class LoopStream
        Inherits WaveStream

        Private sourceStream As WaveStream

        Public Sub New(ByVal sourceStream As WaveStream)
            Me.sourceStream = sourceStream
            Me.EnableLooping = True
        End Sub

        Public Property EnableLooping As Boolean

        Public Overrides ReadOnly Property WaveFormat As WaveFormat
            Get
                Return sourceStream.WaveFormat
            End Get
        End Property

        Public Overrides ReadOnly Property Length As Long
            Get
                Return sourceStream.Length
            End Get
        End Property

        Public Overrides Property Position As Long
            Get
                Return sourceStream.Position
            End Get
            Set(ByVal value As Long)
                sourceStream.Position = value
            End Set
        End Property

        Public Overrides Function Read(ByVal buffer As Byte(), ByVal offset As Integer, ByVal count As Integer) As Integer
            Dim totalBytesRead As Integer = 0

            While totalBytesRead < count
                Dim bytesRead As Integer = sourceStream.Read(buffer, offset + totalBytesRead, count - totalBytesRead)

                If bytesRead = 0 Then

                    If sourceStream.Position = 0 OrElse Not EnableLooping Then
                        Exit While
                    End If

                    sourceStream.Position = 0
                End If

                totalBytesRead += bytesRead
            End While

            Return totalBytesRead
        End Function
    End Class

Loopin MP3 from resources time!

Private waveOut As WaveOut

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If waveOut Is Nothing Then
            Dim mp3file As MemoryStream = New MemoryStream(My.Resources.RequiemAtDusk) '' You can use your MP3 file here
            Dim reader As Mp3FileReader = New Mp3FileReader(mp3file)
            Dim [loop] As LoopStream = New LoopStream(reader)
            waveOut = New WaveOut()
            waveOut.Init([loop])
            waveOut.Play()
        Else
            waveOut.[Stop]()
            waveOut.Dispose()
            waveOut = Nothing
        End If
    End Sub

Upvotes: 1

Related Questions