Search code examples
vb.netnaudio

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

Solution

  • 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