This project has moved. For the latest updates, please go here.

How to close an audio stream the right way to prevent it being in use on copy action

May 22, 2016 at 5:46 PM
So I got my mlayer to work ( code below|)
But every time a play a file and stop it with
waveOut.Stop()
it appears to stay loaded in memory..
Because when i do a runtime copy action of the file...i get an error saying the file is in use by another program...etc. so somehow waveout.stop does not unload the file.
I could try waveout.dispose but that would prevent me from playing the file again afterwarts..since the waveout doesn't excist anymore.
so how to properly unload the mp3 files that where streaming?




'Declare on top:
Imports NAudio.Wave
Imports NAudio.Wave.SampleProviders
'Declare on top in class mainapp ( for my case)
Public waveOut As WaveOut = New WaveOut
Public WithEvents notify as NotifyingSampleProvider = Nothing
Public mp3Reader As Mp3FileReader = Nothing
Public conversionTick_ms As Decimal 'need this for slider

Public Sub Play_Sound(songname As String)
mp3Reader = New Mp3FileReader(songname)
notify = New NotifyingSampleProvider(mp3Reader.ToSampleProvider)

If waveOut.PlaybackState = PlaybackState.Playing Then
    waveOut.Stop()
    mp3Reader.Close()
Else
    waveOut.Init(Notify)
    waveOut.Play()
 conversionTick_ms = (mp3Reader.Position / mp3Reader.CurrentTime.TotalMilliseconds) 'need this for slider
End If
End Sub

'Use the notify event to update your playback position with each processed sample.
'This Event is the most accurate and fires at your Mp3FileReader´s samplerate (usually 44100 times per second).
'For performance reasons you should better count for some samples till update,
'or your Form will redraw the labels 44100 times per second (overkill cpu waste)

Private SampleCounter as Integer = 0
Private Sub notify_Sample(ByVal sender As Object, ByVal e As NAudio.Wave.SampleEventArgs) handles notify.Sample
SampleCounter += 1

'For example wait 1024 samples till updating again.
if SampleCounter >= 1024 then
 SampleCounter = 0

 'Current time:
 CurrentLenLabel.Text = New DateTime(mp3Reader.CurrentTime.Ticks).ToString("HH:mm:ss")

 'Total time:
 TotalLenLabel.Text = New DateTime(mp3Reader.TotalTime.Ticks).ToString("HH:mm:ss") 
'trackposition is the name of the slider(trackbar)
        TrackPosition.Minimum = 0
        TrackPosition.Maximum = mp3Reader.TotalTime.TotalMilliseconds
        TrackPosition.Value = mp3Reader.CurrentTime.TotalMilliseconds
End If
End Sub

'Than i added an event that runs when the position of the slider is changed manualy:
Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button6.Click
   'before changing the position of the mp3, convert milliseconds to ticks... 1ms is approx 176.4 ticks
    mp3Reader.Position = TrackPosition.Value * conversionTick_ms 
End Sub
Coordinator
May 25, 2016 at 2:46 PM
it's the Mp3FileReader that needs disposing. WaveOut.Dispose will close the audio device.
May 26, 2016 at 7:49 PM
Thanks waveout.dispose did the trick