title: Timer in Xamarin.Forms
body: <p>I want to build my own audio player (as there no plugins available). I have used a slider to show progress of the player. I could allow user to go to specific location when the slider is moved but I can not synchronize it with the recording being played.</p>
  
  <p><strong>Form</strong></p>
  
  <pre><code>int i = 1;
  Button btnPlay = new Button
  {
      Text = "Play/Pause",
      Command = new Command(() =&gt;
      {
          DependencyService.Get&lt;IAudio&gt;().PlayMp3File();
          TimeSpan tmspan = new TimeSpan(00, 00, 00);
          label.Text = String.Format("{0:hh\\:mm\\:ss}", tmspan.Add(TimeSpan.FromMilliseconds(i)));
          i++;
          return true;
      })
  };
  Button btnStop = new Button { Text = "Stop" };
  TimeSpan timeSpan = DependencyService.Get&lt;IAudio&gt;().GetInfo();
  Label lblDuration = new Label { Text = String.Format("{0:hh\\:mm\\:ss}", timeSpan) };
  
  var slider = new Slider {
      Minimum = 0,
      Maximum = timeSpan.TotalHours,
  };
  var label = new Label {
      Text = "Slider value is 0",
      FontSize = 25,
      HorizontalOptions = LayoutOptions.Start,
  };
  slider.ValueChanged += 
      (sender, e) =&gt; 
      {
          label.Text = String.Format("Slider value is {0:hh\\:mm\\:ss}", TimeSpan.FromMilliseconds(e.NewValue));
          DependencyService.Get&lt;IAudio&gt;().SeekTo(Convert.ToInt32(e.NewValue));
      };
  MainPage = new ContentPage
  {
      Content = new StackLayout 
      {
          Children = 
          {
              btnPlay,
              btnStop,
              slider,
              lblDuration,
              label
          }
      },  
      Padding = new Thickness (10, Device.OnPlatform (20, 0, 0), 10, 5)
  };
  </code></pre>
  
  <p><strong>Service</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  using Android.App;
  using Android.Content;
  using Android.OS;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  using AudioTest.Droid;
  using Xamarin.Forms;
  using Android.Media;
  using System.Threading.Tasks;
  
  [assembly: Dependency(typeof(AudioService))]
  
  namespace AudioTest.Droid
  {
      public class AudioService : IAudio
      {
          public AudioService() { }
  
          MediaPlayer player = null;
  
          public async Task StartPlayerAsync()
          {
              try
              {
                  if (player == null)
                  {
                      player = new MediaPlayer();
                      player = MediaPlayer.Create(global::Android.App.Application.Context, Resource.Raw.test);
                      player.Start();
                  }
                  else
                  {
                      if (player.IsPlaying == true)
                      {
                          player.Pause();
                      }
                      else
                      {
                          player.Start();
                      }
                  }
              }
              catch (Exception ex)
              {
                  Console.Out.WriteLine(ex.StackTrace);
              }
          }
  
          public void StopPlayer()
          {
              if ((player != null))
              {
                  if (player.IsPlaying)
                  {
                      player.Stop();
                  }
                  player.Release();
                  player = null;
              }
          }
  
          public async Task PlayMp3File()
          {
              await StartPlayerAsync();
          }
  
          public void Stop()
          {
              this.StopPlayer();
          }
  
          public async Task SeekTo( int s)
          {
              await seekTo(s);
          }
  
          public double CurrentPosition()
          {
              if ((player != null))
              {
                  return player.CurrentPosition;
              }
              else
              { return 0; }
          }
  
          private async Task seekTo(int mseconds)
          {
              if (player == null)
              {
                  player = new MediaPlayer();
                  player = MediaPlayer.Create(global::Android.App.Application.Context, Resource.Raw.test);
  
              }
  
              player.SeekTo(mseconds);
              player.Start();
          }
  
  
  
          public TimeSpan GetInfo()
          {
              int arr;
              if (player == null)
              {
                  player = new MediaPlayer();
                  player = MediaPlayer.Create(global::Android.App.Application.Context, Resource.Raw.test);
  
              }
              arr = player.Duration;
  
              return TimeSpan.FromMilliseconds(arr);//TimeSpan.FromMilliseconds(arr).TotalMinutes;
          }
      }
  }
  </code></pre>
  
  <p>Anyone has any idea on how to go about it?</p>
  
  <p><em>Edit</em>
  Screenshot of the screen
  <a href="https://i.stack.imgur.com/wQ88A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wQ88A.png" alt="enter image description here"></a></p>
  
  <p>On play the timer should start and also my slider value should increment. On pause the timer and slider should be paused and on stop it should be stopped.</p>