Hack Forums
[.NET] Naudio Second Audio File Loop for x Time - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Naudio Second Audio File Loop for x Time (/showthread.php?tid=5165574)



Naudio Second Audio File Loop for x Time - OneTXxL - Feb 10, 2016

Hello I encounter a problem during my development, I would like the second audio file is played in a loop but with a waiting time between loops.
I tried LoopStream or For Each, but it did not succeed. Thank you kindly help me
Code
Private Sub button1_Click(sender As Object, e As EventArgs)
    ' read mp3 files from disk
    Dim mpbacground As New Mp3FileReader("D:\song1.mp3")
    Dim mpMessage As New Mp3FileReader("D:\song2.mp3")

    'convert them into wave stream or decode the mp3 file
    Dim background As WaveStream = WaveFormatConversionStream.CreatePcmStream(mpbacground)
    Dim message As WaveStream = WaveFormatConversionStream.CreatePcmStream(mpMessage)

    Dim mixer = New WaveMixerStream32()
    mixer.AutoStop = True

    Dim messageOffset = background.TotalTime
    Dim messageOffsetted = New WaveOffsetStream(message, TimeSpan.FromSeconds(10), TimeSpan.Zero, message.TotalTime.Subtract(TimeSpan.FromSeconds(30)))

    Dim background32 = New WaveChannel32(background)
    background32.PadWithZeroes = False
    ' set the volume of background file
    background32.Volume = 0.4F
    'add stream into the mixer
    mixer.AddInputStream(background32)

    Dim message32 = New WaveChannel32(messageOffsetted)
    message32.PadWithZeroes = False
    ' set the volume of 2nd mp3 song
    message32.Volume = 0.6F
    mixer.AddInputStream(message32)
    Dim wave32 = New Wave32To16Stream(mixer)
    'encode the wave stream into mp3
    Dim mp3Stream = ConvertWavToMp3(wave32)
    ' write mp3 on the disk
    File.WriteAllBytes("D:\output.mp3", mp3Stream.ToArray())

End Sub

''' it converts a wave stream into mp3 stream
Public Shared Function ConvertWavToMp3(wavFile As Wave32To16Stream) As MemoryStream
    Using retMs = New MemoryStream()
        Using wtr = New LameMP3FileWriter(retMs, wavFile.WaveFormat, 128)
            wavFile.CopyTo(wtr)
            Return retMs
        End Using
    End Using
End Function
Mixing two mp3 or wav files using NAudio and Lame
NAudio Codeplex