2016-03-01 71 views
0

我已經使用MPMoviePlayerController實現了我自己的自定義視頻播放器控件,但我遇到了一個問題。如何在用戶點擊暫停按鈕時立即停止UISlider?

當視頻播放和滑塊前進時,如果我點擊暫停按鈕,滑塊會在其停止前移動一秒。

如何確保在用戶點擊暫停時立即停止UISlider

override func viewDidLoad() 
{ 
    super.viewDidLoad() 

    youtubeVideoPlayer.moviePlayer.controlStyle = MPMovieControlStyle.None 

    isSliderTouched = false 

    initUI() 

    tapGesture = UITapGestureRecognizer(target: self, action: "handleSingleTap:") 

    tapGesture2 = UITapGestureRecognizer(target: self, action: "handleSingleTap:") 
} 

override func viewDidAppear(animated: Bool) 
{ 
    super.viewDidAppear(true) 

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateSlider", userInfo: nil, repeats: true) 

    playerSlider.addTarget(self, action: "sliderMoveStart", forControlEvents: .TouchDown) 

    playerSlider.addTarget(self, action: "sliderMoving", forControlEvents: .TouchDragInside) 

    playerSlider.addTarget(self, action: "sliderMoveEnd", forControlEvents: .TouchUpInside) 

    centerButton.addTarget(self, action: "onClick:", forControlEvents: .TouchUpInside) 

    playerSlider.addGestureRecognizer(tapGesture2) 

    lastControlTime = CFAbsoluteTimeGetCurrent() 
} 

override func viewWillDisappear(animated: Bool) 
{ 
    super.viewWillDisappear(true) 

    timer.invalidate() 

    playerSlider.removeTarget(self, action: "sliderMoveStart", forControlEvents: .TouchDown)  

    playerSlider.removeTarget(self, action: "sliderMoving", forControlEvents: .TouchDragInside) 

    playerSlider.removeTarget(self, action: "sliderMoveEnd", forControlEvents: .TouchUpInside) 

    centerButton.removeTarget(self, action: "onClick:", forControlEvents: .TouchUpInside) 

    playerSlider.removeGestureRecognizer(tapGesture2) 
} 

func initUI() 
{ 
    bottomBarBG = UIView(frame: CGRectMake(0, UIScreen.mainScreen().bounds.height - 75, UIScreen.mainScreen().bounds.width, 80)) 

    playerSlider = UISlider() 

    playerSlider.setThumbImage(UIImage(named: "slider"), forState: .Normal) 

    playerSlider.continuous = true 

    playerSlider.highlighted = true 

    playerSlider.frame = CGRectMake(UIScreen.mainScreen().bounds.width * 0.035, 45, UIScreen.mainScreen().bounds.height * 0.52, 22) 

    bottomBarBG.addSubview(playerSlider) 

    centerButton = UIButton(frame: CGRectMake((UIScreen.mainScreen().bounds.width/2) - 25, 0, 50, 50)) 

    centerButton.setImage(UIImage(named: "pause_filled"), forState: .Normal) 

    centerButton.setImage(UIImage(named: "play_filled"), forState: .Selected) 

    bottomBarBG.addSubview(centerButton) 
    view.addSubview(bottomBarBG) 
} 

func handleSingleTap(recogizer: UITapGestureRecognizer) 
{ 
    if recogizer.view == playerSlider 
    { 
     isSliderTouched = true 
     lastControlTime = CFAbsoluteTimeGetCurrent() 

     let translation: CGPoint = recogizer.locationInView(recogizer.view) 

     let rate: Double = Double(translation.x/(UIScreen.mainScreen().bounds.size.width * 0.8)) 

     playerSlider.value = Float(rate) 

     youtubeVideoPlayer.moviePlayer.currentPlaybackTime = Double(youtubeVideoPlayer.moviePlayer.duration) * rate 
    } 
} 

func updateSlider() 
{ 
    if !isSliderTouched 
    { 
     let value: Float = Float(youtubeVideoPlayer.moviePlayer.currentPlaybackTime/youtubeVideoPlayer.moviePlayer.duration) 

     playerSlider.setValue(value, animated: true) 
    } 
} 

func sliderMoveStart() 
{ 
    isSliderTouched = true 
} 

func sliderMoving() 
{ 
    lastControlTime = CFAbsoluteTimeGetCurrent() 

    isSliderTouched = true 

    let time: Double = youtubeVideoPlayer.moviePlayer.duration * Double(playerSlider.value) 
} 

func sliderMoveEnd() 
{ 
    youtubeVideoPlayer.moviePlayer.currentPlaybackTime = youtubeVideoPlayer.moviePlayer.duration * Double(playerSlider.value) 
} 

func onClick(sender: UIButton) 
{ 
    if sender == centerButton 
    { 
     lastControlTime = CFAbsoluteTimeGetCurrent() 

     if sender.selected 
     { 
      youtubeVideoPlayer.moviePlayer.play() 
      sender.selected = false 
     } 
     else 
     { 
      youtubeVideoPlayer.moviePlayer.pause() 
      sender.selected = true 
     } 
    } 
} 

僅供參考,centerButton是我的播放/暫停按鈕。

謝謝。

+1

您需要在暫停時停止計時器。 – dichen

回答

1

在暫停時添加timer.invalidate(),並再次播放定時器。