我如何将这四行代码从AVAudioPlayer转换为AVPlayer?

时间:2016-04-22 04:52:42

标签: ios xcode swift swift2 avplayer

我需要将代码从AVAudioPlayer更改为AVPlayer,因为它支持快退,而AVAudioPlayer则不支持。我遇到了这四行代码的问题,这些代码寻找使用滑块播放的音频。这是我的代码:

func changeAudioTime() {

    //musicPlayer is declared as AVAudioPlayer and I want to change it to  AVPlayer and I named the variable player

    musicPlayer.prepareToPlay()
    musicPlayer.currentTime = NSTimeInterval(slider.value)
    musicPlayer.numberOfLoops = -1
    slider.value = Float(musicPlayer.currentTime)

}

1 个答案:

答案 0 :(得分:1)

在这两种情况下都可以设置时间:

self.player.seekToTime(CMTimeMake(Int64(slider.value),1))
slider.value = Float(CMTimeGetSeconds(self.player.currentTime()))

对于循环你可以尝试这样,因为没有直接的循环方法,直到你想重复你可以设置标志/观察者并重复歌曲如果重复关闭你不能执行观察者/寻求

NSNotificationCenter.defaultCenter().addObserver(self,
selector: "playerItemDidReachEnd:",name: AVPlayerItemDidPlayToEndTimeNotification,
object: self.player.currentItem)

func playerItemDidReachEnd(notification: NSNotification) {
    self.player.seekToTime(kCMTimeZero)
    self.player.play()
}