1

I'm trying to have avPlayer restart the current song when the skip back button is selected if it's after about 5 seconds into the song, and jump to times according to where the slider is moved to. Here's my code for these functions:

@IBAction func buttonBackTouchDown(sender: AnyObject) {
    if (Float(CMTimeGetSeconds((avPlayerItem?.currentTime())!)) > 5) {
        //restartSong = true
        avPlayer.currentItem!.seekToTime(kCMTimeZero)
        //self.loadSongValues()
        //restartSongTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("setRestartSongToFalse"), userInfo: nil, repeats: false)
    }
    else if (Float(CMTimeGetSeconds((avPlayerItem?.currentTime())!)) < 5) {
        self.previousSong()
    }
}


@IBAction func sliderTouchDown(sender: AnyObject) {
        self.sliderTimer.invalidate()
        avPlayer.pause()
        self.overlayOn()
    }


@IBAction func sliderTouchUpInside(sender: AnyObject) {
        //restartSongTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("setRestartSongToFalse"), userInfo: nil, repeats: false)
        //NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("startSliderTimer"), userInfo: nil, repeats: false)
        self.startLabelTimer()
        avPlayer.play()
        self.updateNowPlayingInfoCenter()
        self.overlayOff()
    }

The problem I'm having is that if the song is over 1 minute in, or it's almost finished and I move the slider too far back, it SOMETIMES triggers this observer:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "skipSong", name: AVPlayerItemDidPlayToEndTimeNotification, object: avPlayerItem)

I tried looking in Apple's references and looked to other questions here on StackOverflow, but couldn't find anything.

Maybe I'm not understanding how to properly use CMTime, or something is weird with the files, but this is really weird to me.

Any help is appreciated. Thanks in advance!

4

1 回答 1

0

我也遇到了很多关于 AVPlayer 的问题,尤其是 SeekToTime 方法。

当你这样做

avPlayer.currentItem!.seekToTime(kCMTimeZero)

avPlayer 正在跳转到 0 帧,而不是应该的第一帧。对于 AVPlayer,第 0 帧和最后一帧似乎是相同的。尝试以下操作:(比如跳到时间 0.001 而不是 0)

        let seekTime : CMTime = CMTimeMake(1, 1000)
        avPlayer!.seekToTime(seekTime)
于 2016-04-12T15:37:14.573 回答