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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.