SeekBar.IOnSeekBarChangeListener,seekBar无法运行

时间:2016-04-30 04:23:36

标签: xamarin xamarin.android

在进度级别更改后通知客户端的回调。这包括用户通过触摸手势或箭头键/轨迹球发起的更改以及以编程方式启动的更改。

内部OnCreate

public class AudioPlayer : Activity,SeekBar.IOnSeekBarChangeListener
seekBar.SetOnSeekBarChangeListener (this);
        seekBar.ProgressChanged+= (object sender, SeekBar.ProgressChangedEventArgs e) => {
            int progress=(int)(utils.getProgressPercentage(player.CurrentPosition,player.Duration));
            seekBar.Progress = progress;

        };

public void OnStartTrackingTouch(SeekBar seekBar) {}

public void OnStopTrackingTouch(SeekBar seekBar) {}


public int getProgressPercentage(int currentDuration, int totalDuration)
{
    int percentage;

    int currentSeconds = (int)(currentDuration / 1000);
    int totalSeconds = (int)(totalDuration / 1000);

    //calculating percentage

    percentage = (((int)currentSeconds) / totalSeconds) * 100;

    return percentage;
}

在播放曲目时,seekBar仍会停止。

1 个答案:

答案 0 :(得分:2)

你想要做的就是创建一个定时器,它会像这样更新seekBar:

的活动:

using System;
using Android.App;
using Android.Widget;
using Android.OS;
using Java.Lang;
using Android.Content;
using Android.Graphics.Drawables;
using Java.Util;

namespace PlayVideo
{
    [Activity (Label = "PlayVideo", MainLauncher = true)]
    public class Activity1 : Activity
    {
        VideoView videoView;
        SeekBar seekBar;
        System.Timers.Timer timer;
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
            videoView = FindViewById<VideoView> (Resource.Id.SampleVideoView);
            videoView.SetMediaController(new MediaController(this));
            videoView.SetVideoPath ($"android.resource://{PackageName}/{Resource.Raw.output2}");
            videoView.Start ();

            seekBar = FindViewById<SeekBar> (Resource.Id.seekBar);
            seekBar.Max = videoView.Duration;
            seekBar.StartTrackingTouch += (object sender, SeekBar.StartTrackingTouchEventArgs e) => 
            {
                timer.Enabled = false;
            };
            seekBar.StopTrackingTouch += (object sender, SeekBar.StopTrackingTouchEventArgs e) => 
            {
                videoView.SeekTo (e.SeekBar.Progress);
                timer.Enabled = true;           
            };
            UpdateProgressBar ();
        }


        private void UpdateProgressBar() {
            timer = new System.Timers.Timer();
            timer.Interval = 100; 
            timer.Elapsed += OnTimedEvent;
            timer.Enabled = true;
        }

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

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <VideoView
        android:id="@+id/SampleVideoView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <SeekBar
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar" />
</LinearLayout>

它看起来像这样:

enter image description here