我正在 iOS 中创建视频播放器应用程序,我使用 MpMoviePlayer。一切都运转良好。
我的问题是,我想在我的应用程序中添加现在播放功能,就像音乐应用程序一样,这里是链接 https://itunes.apple.com/in/app/musi-unlimited-free-music/id591560124?mt=8
我现在正在我的应用程序中实现播放功能,但它运行不顺畅。
你能帮我建议我该怎么做吗???
现在正在播放点击 btn 操作
@IBAction func nowPlaying(sender: AnyObject) {
dataVideoName2.removeAll()
dataAuthorName2.removeAll()
dataVideoId2.removeAll()
let vc = storyboard?.instantiateViewControllerWithIdentifier("player") as! SimpleplayerViewController
vc.videoID = dataVideoId2
vc.videoTitle = dataVideoName2
vc.artist = dataAuthorName2
vc.thumbURL = dataVideoThumb
vc.dismiss = 1
navigationController?.pushViewController(vc, animated: true)
}
最佳答案
伊尔沙德,
您可以将 NSTimer 添加到您的视频中以定期检查时间。
func updateAVDTag() {
let currentPT:Float64 = CMTimeGetSeconds((self.streamPlayer.player?.currentTime())!)
let currentPlayerTString = String(currentPT)
currentPlayerTime = String(currentPlayerTString.characters.prefix(4))
}
self.playingAV = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "updateAVDTag", userInfo: nil, repeats: true)
并在完成时通知您。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "finishedPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
func finishedPlaying(myNotification:NSNotification) {
self.streamPlayer.view.removeFromSuperview()
playingAV.invalidate()
playingAV = nil
print("VIDEO finished")
}
但要小心,确保更新 UI 的任何代码都在主线程上运行,并且您的播放器不是本地变量。确保您在正确的线程上运行 UI 更新的代码。
dispatch_async(dispatch_get_main_queue()) {
// Update UI
}
关于ios - 在 iOS 中添加“立即播放”功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35740632/