为啥我的时间 UILabel 从暂停状态恢复播放时会跳过 2 秒?
Posted
技术标签:
【中文标题】为啥我的时间 UILabel 从暂停状态恢复播放时会跳过 2 秒?【英文标题】:Why does my time UILabels skip 2 seconds when it resumes play from a pause state?为什么我的时间 UILabel 从暂停状态恢复播放时会跳过 2 秒? 【发布时间】:2016-03-07 07:00:12 【问题描述】:我在使用 MPMoviePlayerController
时遇到了一个奇怪的问题。我有一个代表当前播放时间的currentTimeLabel
和一个代表剩余播放时间的endTimeLabel
。使用NSTimer
每 1 秒触发一次更新标签的函数。
这与 UISlider
在视频播放时每秒递增的情况相吻合。但是,问题是,当视频正在播放时,我点击暂停按钮,当我再次点击播放按钮时,我的currentTimeLabel
和endTimeLabel
从上次停止的地方跳了 2 秒。
这是提供的代码:
override func viewDidLoad()
super.viewDidLoad()
print("viewDidLoad")
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "moviePlayerPlaybackStateDidChangeNotification:",
name: MPMoviePlayerPlaybackStateDidChangeNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "moviePlayerLoadStateDidChangeNotification:",
name: MPMoviePlayerLoadStateDidChangeNotification,
object: nil)
setUpVideoPlayer()
isPlaybackSliderTouched = false
isPauseButtonTouched = false
override func viewDidAppear(animated: Bool)
super.viewDidAppear(true)
print("viewDidAppear")
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updatePlaybackSlider", userInfo: nil, repeats: true)
playPauseButton.addTarget(self, action: "onClick:", forControlEvents: .TouchUpInside)
func convertSecondsToHHMMSS(seconds: Double) -> String
let time: Int = Int( floor(seconds) )
print("time = \(time)")
// Convert to hours
let hh: Int = time / 3600
// Convert to minutes
let mm: Int = (time / 60) % 60
// Convert to seconds
let ss: Int = time % 60
print("seconds = \(ss)")
if hh > 0
return String(format: "%02d:%02d:%02d", hh, mm, ss)
else
return String(format: "%02d:%02d", mm, ss )
func updatePlaybackSlider()
if isPlaybackSliderTouched == false
print("PLAYBACKSLIDER IS NOT TOUCHED!")
print("youtubeVideoPlayer.moviePlayer.currentPlaybackTime = \(youtubeVideoPlayer.moviePlayer.currentPlaybackTime)")
print("youtubeVideoPlayer.moviePlayer.duration = \(youtubeVideoPlayer.moviePlayer.duration)")
let sliderValue: Float = Float(youtubeVideoPlayer.moviePlayer.currentPlaybackTime / youtubeVideoPlayer.moviePlayer.duration)
print("sliderValue = \(sliderValue)")
if !sliderValue.isNaN
self.playbackSlider.setValue(sliderValue, animated: true)
self.currentTimeLabel.text = self.convertSecondsToHHMMSS(self.youtubeVideoPlayer.moviePlayer.currentPlaybackTime)
self.endTimeLabel.text = "-" + self.convertSecondsToHHMMSS(self.youtubeVideoPlayer.moviePlayer.duration - self.youtubeVideoPlayer.moviePlayer.currentPlaybackTime)
print("currentTimeLabel.text = \(currentTimeLabel.text)")
print("endTimeLabel.text = \(endTimeLabel.text)")
func onClick(sender: UIButton)
print("onClick")
if sender == playPauseButton
print("playPauseButton touched")
if sender.selected
print("player play")
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updatePlaybackSlider", userInfo: nil, repeats: true)
isPauseButtonTouched = false
youtubeVideoPlayer.moviePlayer.play()
sender.selected = false
else
print("player paused")
isPauseButtonTouched = true
timer.invalidate()
timer = nil
youtubeVideoPlayer.moviePlayer.pause()
sender.selected = true
func moviePlayerPlaybackStateDidChangeNotification(notification: NSNotification)
print("moviePlayerPlaybackStateDidChangeNotification")
let state: MPMoviePlaybackState = youtubeVideoPlayer.moviePlayer.playbackState
switch state
case MPMoviePlaybackState.Stopped:
print("stopped")
break
case MPMoviePlaybackState.Interrupted:
print("interrupted")
break
case MPMoviePlaybackState.SeekingForward:
print("forward")
break
case MPMoviePlaybackState.SeekingBackward:
print("backward")
break
case MPMoviePlaybackState.Paused:
print("paused")
break
case MPMoviePlaybackState.Playing:
print("playing")
break
func moviePlayerLoadStateDidChangeNotification(notification: NSNotification)
print("moviePlayerLoadStateDidChangeNotification")
let state: MPMovieLoadState = youtubeVideoPlayer.moviePlayer.loadState
switch state
case MPMovieLoadState.Playable:
print("playing")
break
case MPMovieLoadState.PlaythroughOK:
print("PlaythroughOK")
break
case MPMovieLoadState.Stalled:
print("stalled")
break
case MPMovieLoadState.Unknown:
print("unknown")
break
default:
break
这是播放几秒钟的视频输出:
viewDidLoad
setUpVideoPlayer
viewDidAppear
moviePlayerLoadStateDidChangeNotification
time = 0
seconds = 0
time = 29
seconds = 29
playing
moviePlayerLoadStateDidChangeNotification
moviePlayerPlaybackStateDidChangeNotification
playing
PLAYBACKSLIDER IS NOT TOUCHED!
youtubeVideoPlayer.moviePlayer.currentPlaybackTime = 0.0
youtubeVideoPlayer.moviePlayer.duration = 29.8833333333333
sliderValue = 0.0
time = 0
seconds = 0
time = 29
seconds = 29
currentTimeLabel.text = Optional("00:00")
endTimeLabel.text = Optional("-00:29")
PLAYBACKSLIDER IS NOT TOUCHED!
youtubeVideoPlayer.moviePlayer.currentPlaybackTime = 0.484628237
youtubeVideoPlayer.moviePlayer.duration = 29.8833333333333
sliderValue = 0.016255
time = 0
seconds = 0
time = 29
seconds = 29
currentTimeLabel.text = Optional("00:00")
endTimeLabel.text = Optional("-00:29")
PLAYBACKSLIDER IS NOT TOUCHED!
youtubeVideoPlayer.moviePlayer.currentPlaybackTime = 1.484112736
youtubeVideoPlayer.moviePlayer.duration = 29.8833333333333
sliderValue = 0.0496863
time = 1
seconds = 1
time = 28
seconds = 28
currentTimeLabel.text = Optional("00:01")
endTimeLabel.text = Optional("-00:28")
onClick
playPauseButton touched
player paused
moviePlayerPlaybackStateDidChangeNotification
paused
isPauseButtonTouched = true
onClick
playPauseButton touched
player play
moviePlayerPlaybackStateDidChangeNotification
playing
PLAYBACKSLIDER IS NOT TOUCHED!
youtubeVideoPlayer.moviePlayer.currentPlaybackTime = 3.34555252
youtubeVideoPlayer.moviePlayer.duration = 29.8833333333333
sliderValue = 0.111982
time = 3
seconds = 3
time = 26
seconds = 26
currentTimeLabel.text = Optional("00:03")
endTimeLabel.text = Optional("-00:26")
moviePlayerPlaybackStateDidChangeNotification
paused
正如您在输出中看到的,currentTimeLabel.text = Optional("00:01")
是我点击暂停的时间。当我点击播放时,输出应该是currentTimeLabel.text = Optional("00:02")
,但它显示currentTimeLabel.text = Optional("00:03")
。我的endTimeLabel
也跳过了 2 秒。
为什么会发生这种情况,我该如何解决?
谢谢
【问题讨论】:
【参考方案1】:恢复计时器在播放状态。或许能解决问题。
case MPMoviePlaybackState.Playing:
print("playing")
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updatePlaybackSlider", userInfo: nil, repeats: true)
break
【讨论】:
解决了这个问题,但是又出现了另一个问题。现在我的滑块速度是原来的两倍,而不是每秒移动一次。 您是否从 Onclick 方法中删除了计时器?以上是关于为啥我的时间 UILabel 从暂停状态恢复播放时会跳过 2 秒?的主要内容,如果未能解决你的问题,请参考以下文章