Imports System.Windows.Threading

Partial Public Class SilverlightAudio
  Inherits UserControl
  Private temporizador As DispatcherTimer

  Public Sub New()
    InitializeComponent()
    temporizador = New DispatcherTimer()
    temporizador.Interval = TimeSpan.FromSeconds(0.1)
    AddHandler temporizador.Tick, AddressOf Temporizador_Tick
    sliderPosicion.AddHandler(MouseLeftButtonDownEvent,
                              New MouseButtonEventHandler(AddressOf sliderPosicion_MouseLeftButtonDown),
                              True)
    sliderPosicion.AddHandler(MouseLeftButtonUpEvent,
                              New MouseButtonEventHandler(AddressOf sliderPosicion_MouseLeftButtonUp),
                              True)
  End Sub

  Private Sub Temporizador_Tick(sender As Object, e As EventArgs)
    tbkEstado.Text = meReproductor.Position.ToString().TrimEnd(New Char() {"0"c})
    sliderPosicion.Value = meReproductor.Position.TotalSeconds
  End Sub

  Private Sub meReproductor_MediaOpened(sender As Object, e As RoutedEventArgs)
    sliderPosicion.Maximum = meReproductor.NaturalDuration.TimeSpan.TotalSeconds
  End Sub

  Private Sub btReproducir_Click(sender As Object, e As RoutedEventArgs)
    temporizador.Start()
    meReproductor.Play()
  End Sub

  Private Sub btParar_Click(sender As Object, e As RoutedEventArgs)
    meReproductor.Stop()
    temporizador.Stop()
  End Sub

  Private Sub btPausa_Click(sender As Object, e As RoutedEventArgs)
    meReproductor.Pause()
    temporizador.Stop()
  End Sub

  Private Sub meReproductor_MediaEnded(sender As Object, e As RoutedEventArgs)
    If chbRepetir.IsChecked = True Then
      meReproductor.Position = TimeSpan.Zero
      meReproductor.Play()
    Else
      temporizador.Stop()
    End If
  End Sub

  Private Sub chbSilencio_Click(sender As Object, e As RoutedEventArgs)
    meReproductor.IsMuted = CBool(chbSilencio.IsChecked)
  End Sub

  Private Sub meReproductor_MediaFailed(sender As Object, e As ExceptionRoutedEventArgs)
    tbkEstado.Text = e.ErrorException.Message
  End Sub

  Private Sub meReproductor_CurrentStateChanged(sender As Object, e As RoutedEventArgs)
    tbkEstado.Text = meReproductor.CurrentState.ToString()
  End Sub

  Private Sub sliderVolumen_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double))
    If meReproductor IsNot Nothing Then
      meReproductor.Volume = sliderVolumen.Value
    End If
  End Sub

  Private Sub sliderBalance_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double))
    If meReproductor IsNot Nothing Then
      meReproductor.Balance = sliderBalance.Value
    End If
  End Sub

  Private Sub sliderPosicion_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs)
    btPausa_Click(btPausa, Nothing)
  End Sub

  Private Sub sliderPosicion_MouseLeftButtonUp(sender As Object, e As MouseButtonEventArgs)
    meReproductor.Position = TimeSpan.FromSeconds(sliderPosicion.Value)
    meReproductor.Play()
    temporizador.Start()
  End Sub
End Class
