当媒体播放器在xamarin android上播放时,SeekBar不会运行

时间:2016-04-29 02:43:26

标签: xamarin xamarin.android

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;
}
public void UpdatedTimerTask()
{
    //Displaying time
    //txtCurrentTimer.Text = utils.miliSecondsTotimer (player.CurrentPosition);
    //txtTotalTimer.Text = utils.miliSecondsTotimer (player.Duration);

    //Updating progress bar(seekbar)
    int progress=(int)(utils.getProgressPercentage(player.CurrentPosition,player.Duration));
    seekBar.Progress = progress;
}

void SeekBar_ProgressChanged (object sender, SeekBar.ProgressChangedEventArgs e)
{
    UpdatedTimerTask ();
}

public void StartMedia(string url_string)
{

    player = new MediaPlayer ();
    seekBar.Progress = 0;
    seekBar.Max = 100;
    player.Reset ();
    player.SetAudioStreamType (Stream.Music);
    player.SetDataSource(url_string);
    player.Prepare();
    player.Start ();
    imgPlayorPause.SetImageResource (Resource.Drawable.ic_pause_black_36dp);
    UpdatedTimerTask ();
}

seekBar.SetOnSeekBarChangeListener (this);有一些无效的论点。

当媒体播放器正在播放时,SeekBar不会运行。

1 个答案:

答案 0 :(得分:1)

您有两种选择:

SetOnSeekBarChangeListener()

如果您想使用SetOnSeekBarChangeListener,则必须在IOnSeekBarChangeListener课程中实施this(通常是您的活动。劣势,这是,你只能有一个事件监听器。

[Activity]
public class MyActivity : Activity, SeekBar.IOnSeekBarChangeListener
{
    protected override void OnCreate(Bundle bundle)
    {
        // ...
        seekbar.SetOnSeekBarChangeListener(this);
    }

    public void OnProgressChanged(SeekBar seekBar, int progress, bool fromUser)
    {
        // do some stuff
    }

    public void OnStartTrackingTouch(SeekBar seekBar)
    {
    }

    public void OnStopTrackingTouch(SeekBar seekBar)
    {
    }
}

ProgressChanged事件

Xamarin将名为SetXyzListener的Java方法映射到名为Xyz的事件。 如果您想使用ProgressChanged,则必须使用seekbar.ProgressChanged += SeekbarOnProgressChanged注册您的处理程序。 缺点是,当您不再需要该事件时,必须确保使用seekbar.ProgressChanged -= SeekbarOnProgressChanged删除处理程序。您应该在已添加处理程序的生命周期方法的对应项中执行此操作。在以下示例中,我使用了OnResumeOnPause

[Activity]
public class MyActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        // ...
    }

    protected override void OnResume()
    {
        base.OnResume();
        seekbar.ProgressChanged += SeekbarOnProgressChanged;
    }

    protected override void OnPause()
    {
        seekbar.ProgressChanged -= SeekbarOnProgressChanged;
        base.OnPause();
    }

    private void SeekbarOnProgressChanged(object sender, SeekBar.ProgressChangedEventArgs progressChangedEventArgs)
    {
        // do some stuff
    }
}