2

I'm creating a simple app that plays music in Xamarin Android. I can't get the seekbar to update itself with the song's progress. In JAVA the Runnable interface is used.

Here is my code snippet

`protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.MediaPlayer);

            player = Android.Media.MediaPlayer.Create(this, Resource.Raw.test);

            playButton = FindViewById<Button>(Resource.Id.play);
            pauseButton = FindViewById<Button>(Resource.Id.pause);
            stopButton = FindViewById<Button>(Resource.Id.stop);

                playButton.Click += OnPlayClick;
                pauseButton.Click += OnPauseClick;
                stopButton.Click += OnStopClick;

            seekBar = FindViewById<SeekBar>(Resource.Id.seekBar);
        }

        private void OnPlayClick(object sender, EventArgs e)
        {

            player.Start();
        }

        private void OnPauseClick(object sender, EventArgs e)
        {
            player.Pause();
        }

        private void OnStopClick(object sender, EventArgs e)
        {
            player.Stop();
            player.Prepare();
            player.SeekTo(0);
        }`

This is the basic player implementation.

This is the java version of what I want to do. How to use a Seekbar in android as a seekBar as well as a progressBar simultaneously?

Community
  • 1
  • 1
radu
  • 35
  • 6
  • Answered a similar question [hear](http://stackoverflow.com/questions/10000400/mediaplayer-progressbar/10001044#10001044) – Grzegorz Jan 31 '17 at 19:44

1 Answers1

2

You need to add a timer when you start your player.

Try the following code:

    public class MainActivity : Activity
    {
        Button playButton;
        Button pauseButton;
        Button stopButton;
        SeekBar seekBar;
        Android.Media.MediaPlayer player;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            player = Android.Media.MediaPlayer.Create(this, Resource.Raw.hello);

            playButton = FindViewById<Button>(Resource.Id.play);
            pauseButton = FindViewById<Button>(Resource.Id.pause);
            stopButton = FindViewById<Button>(Resource.Id.stop);

            playButton.Click += OnPlayClick;
            pauseButton.Click += OnPauseClick;
            stopButton.Click += OnStopClick;



            seekBar = FindViewById<SeekBar>(Resource.Id.myseekBar);
        }



        private void OnPlayClick(object sender, System.EventArgs e)
        {

            player.Start();
            seekBar.Max = player.Duration;
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 10;
            timer.Elapsed += OnTimedEvent;
            timer.Enabled = true;
        }

        private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            seekBar.Progress = player.CurrentPosition;
        }

        private void OnPauseClick(object sender, System.EventArgs e)
        {
            player.Pause();
        }

        private void OnStopClick(object sender, System.EventArgs e)
        {
            player.Stop();
            player.Prepare();
            player.SeekTo(0);
        }
    }
}

When you click playButton the timer event will be called and update the seekbar UI timely.

enter image description here

Mike Ma
  • 2,017
  • 1
  • 12
  • 17
  • @radu you are welcome if it is helpful to you, do not forget to mark this answer thanks. – Mike Ma Feb 01 '17 at 07:05
  • One question though. This doesn't look like it actually seeks. I mean if I move the seekbar to get to that time in the song. However I think this could be implemented by simply adding player.SeekTo(seekBar.progreee). Does this sound ok to you? – radu Feb 01 '17 at 07:59
  • @radu you should stop timer when you move the seekBar. Add the change listener to the seekbar change the player seek in the listener. I suggest you to stop timer when you click the stop button or move the seekbar. The sounds should be fine. – Mike Ma Feb 01 '17 at 08:27
  • Didn't work. I implemented the listener as you said, but whenever I try to drag to seek it just goes back to it's position. Think I might have missed something? – radu Feb 03 '17 at 09:20
  • @radu the seek bar goes back to start position when you drag it? – Mike Ma Feb 03 '17 at 09:38
  • say for example the song is at 20% progress (so is the seek bar). when dragging it further along the way it goes back to the moment I started dragging (the 20% mentioned above). Hope this makes sense. When I get home I will post a gif and the code snippet of what I've done – radu Feb 03 '17 at 13:12
  • @radu You can send a new post about the `seek bar control music progress` and add some description . And give a link to me. – Mike Ma Feb 06 '17 at 01:32
  • Solved it Mike. Thanks for your help – radu Feb 06 '17 at 08:15