This project has moved and is read-only. For the latest updates, please go here.

Timer in NAudio

Jan 4, 2012 at 6:55 AM
Edited Jan 4, 2012 at 7:02 AM

I try to develop school bell application using NAudio with timer. I compare the timer with data stored in database. Here the code ( I take code from the example from NAudio):

 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick  
Label1.Text = TimeOfDay bellEnabled = True For Each r As DataGridViewRow In DataGridView1.Rows Dim jam As String = Convert.ToString(r.Cells) If r.Cells("jm").Value = Label2.Text And bellEnabled = True Then 'taken from datagridview Dim nmFile As String nmFile = "E:\bell1.mp3" waveOutDevice = New WaveOut(callbackInfo) mainOutputStream = CreateInputStream(nmFile) waveOutDevice.Init(mainOutputStream) waveOutDevice.Play() End If Next End Sub

This code didn't work fine. The output sound is 'broken' (sorry for my bad English). But when I set 'bellEnabled' and 'timer1.Enabled' to false the output sound was perfect. How solve this..? I need the timer to run continuously and compare it with database...

Thanks for help

Jan 5, 2012 at 6:44 PM
Edited Jan 5, 2012 at 6:46 PM

If I understand what you are trying to do, then something like this should work.

 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick  
        Label1.Text = TimeOfDay
        bellEnabled = True

        For Each r As DataGridViewRow In DataGridView1.Rows
            Dim jam As String = Convert.ToString(r.Cells)

            If r.Cells("jm").Value = Label2.Text And bellEnabled = True Then 'taken from datagridview
               PlayBell()
            End If
        Next
End Sub
Sub New()
       Dim nmFile As String
       nmFile = "E:\bell1.mp3"
       waveOutDevice = New WaveOut(callbackInfo)
       mainOutputStream = CreateInputStream(nmFile)
       waveOutDevice.Init(mainOutputStream)
End Sub
Private Sub PlayBell()
       If waveOutDevice.PlaybackState <> Playing Then
            waveOutDevice.Play()
       End If
End Sub
Jan 6, 2012 at 5:26 AM
zsb wrote:

If I understand what you are trying to do, then something like this should work.

 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick  
        Label1.Text = TimeOfDay
        bellEnabled = True

        For Each r As DataGridViewRow In DataGridView1.Rows
            Dim jam As String = Convert.ToString(r.Cells)

            If r.Cells("jm").Value = Label2.Text And bellEnabled = True Then 'taken from datagridview
               PlayBell()
            End If
        Next
End Sub
Sub New()
       Dim nmFile As String
       nmFile = "E:\bell1.mp3"
       waveOutDevice = New WaveOut(callbackInfo)
       mainOutputStream = CreateInputStream(nmFile)
       waveOutDevice.Init(mainOutputStream)
End Sub
Private Sub PlayBell()
       If waveOutDevice.PlaybackState <> Playing Then
            waveOutDevice.Play()
       End If
End Sub

 

Sorry, I new in vb.net. I've added 'InitializeComponent()' since the code gives me warning "should call InitializeComponent method." But I still can't get it worked well. In the database I put 3 data (08:00:00, 08:20:00, and 08:40:00). The sound play well when the timer match the 1st data (08:00:00), but not for the next two data. There's no sound at all

Jan 6, 2012 at 2:42 PM
djtechwork wrote:
Sorry, I new in vb.net. I've added 'InitializeComponent()' since the code gives me warning "should call InitializeComponent method." But I still can't get it worked well. In the database I put 3 data (08:00:00, 08:20:00, and 08:40:00). The sound play well when the timer match the 1st data (08:00:00), but not for the next two data. There's no sound at all

I would recommend that since you are new to vb.net to forget about using a database for now. Try using an array with the times you want to check for.

Dim timeArray() as String = {"08:00:00", "08:20:00", "08:04:00"}

Then simply loop through the array to check the time.

For c = 0 to timeArray.Count - 1
    If timeArray(c) = TimeofDay Then
       PlayBell()
    End If
Next c

 

 

On a side note, please remember we are here to help with NAudio(not with everything else)

Jan 6, 2012 at 7:03 PM
zsb wrote:
djtechwork wrote:
Sorry, I new in vb.net. I've added 'InitializeComponent()' since the code gives me warning "should call InitializeComponent method." But I still can't get it worked well. In the database I put 3 data (08:00:00, 08:20:00, and 08:40:00). The sound play well when the timer match the 1st data (08:00:00), but not for the next two data. There's no sound at all

I would recommend that since you are new to vb.net to forget about using a database for now. Try using an array with the times you want to check for.

 

Dim timeArray() as String = {"08:00:00", "08:20:00", "08:04:00"}

 

Then simply loop through the array to check the time.

 

For c = 0 to timeArray.Count - 1
    If timeArray(c) = TimeofDay Then
       PlayBell()
    End If
Next c

 

 

 

On a side note, please remember we are here to help with NAudio(not with everything else)

Thanks for suggestion.

Sorry if my question isn't in proper place..