ios - 在 iOS 中添加“立即播放”功能

标签 ios swift mpmovieplayercontroller

我正在 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/

相关文章:

ios - Xcode 5.1 不能与 iPhone 7.1 设备一起使用?

ios - UILongPressGestureRecognizer 识别其 View 外的触摸

ios - 在 Swift 中将字符串转换为数组 : Time Complexity

c++ - 在swift代码中加载静态库

swift - 将非转义闭包转换为转义闭包

iOS HTTP Live Streaming - 当比特率未知时请求的字节范围?

ios - 检查 CBPeripheralManager.authorizationStatus() 现在它已被弃用

ios - 不能有与参数类型同名的函数

ios - MPMoviePlayerController:不播放视频

iphone - 如何更改 MPMoviePlayerViewController 的 UIModalTransitionStyle