2016-04-30 36 views
1

當進度水平已經改變了通知客戶端的回調。這包括用戶通過觸摸手勢或箭頭鍵/軌跡球發起的更改以及以編程方式啓動的更改。SeekBar.IOnSeekBarChangeListener,搜索欄無法運行

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; 
} 

雖然曲目播放,搜索欄仍然停止。

回答

2

想你想要做的是創建一個計時器,將更新這樣的搜索條:

活動:

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