Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: using a scroll bar to navigate true the mp3 file playing..in VB.NET

$
0
0
Got it al to work
here is my working code.
'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 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()
    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 * 176.4
    End Sub

The one concern i have, the number I use to convert millisecs to ticks..is that approx the same on every machine or does it vary with speed ...? because than i have a problem when i distribute it....

Viewing all articles
Browse latest Browse all 5831

Latest Images

Trending Articles



Latest Images