【问题标题】:AVPlayer is not working when I'm trying to use with this YTVimeoExtractor当我尝试与此 YTVimeoExtractor 一起使用时,AVPlayer 无法正常工作
【发布时间】:2016-05-11 15:35:58
【问题描述】:

当我尝试通过 AVPlayer 播放视频时,视频加载了一段时间(加载符号出现在播放器的顶部)然后突然停止并显示带有交叉的播放图标。不知道有什么问题?我可以获取视频信息,但无法播放视频。 我会展示我所做的。谁能回答我的问题,非常感谢帮助。

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

var playerVC : AVPlayerViewController!
var playerItem : AVPlayerItem!
var player : AVPlayer!
var playerLayer: AVPlayerLayer!


@IBOutlet weak var videoURL: UITextField!
@IBOutlet weak var videoTitle: UILabel!
@IBAction func playVideo(sender: AnyObject) {

    YTVimeoExtractor.sharedExtractor().fetchVideoWithVimeoURL(self.videoURL.text!, withReferer: nil, completionHandler: {(video, error) -> Void in

        if video != nil {


            // https://vimeo.com/165891648
            self.videoTitle.text = video?.title

            print("hello: \(self.videoTitle.text)")

            let url = NSURL(string: self.videoURL.text!)


//                let url = NSURL.init(fileURLWithPath: self.videoURL.text!)

            self.playerItem = AVPlayerItem.init(URL: url!)
            self.player = AVPlayer.init(playerItem: self.playerItem)
            self.playerVC = AVPlayerViewController.init();
            self.playerVC.player = self.player;
            self.player.currentItem!.playbackLikelyToKeepUp

            self.presentViewController(self.playerVC, animated: true) { () -> Void in
                self.playerVC.player?.play()
            }

        }else {

            let alert = UIAlertController(title: error!.localizedDescription, message:  error!.localizedFailureReason, preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)

        }

    })



}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


}

override func viewDidAppear(animated: Bool) {

        }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
   }    
}

这是我在模拟器上得到的。

我用这个https://github.com/lilfaf/YTVimeoExtractor 来做这个示例项目。你可以试试看告诉我。

【问题讨论】:

  • self.videoTitle.text = video?.title的值是多少
  • 是视频的标题
  • 网址是什么?
  • 它是视频网址,我在文本字段中输入它,然后单击模拟器中的播放按钮,最终将播放视频,但肯定不是!
  • 我们需要知道视频的url来检查它是否有效。

标签: swift github avplayer vimeo avkit


【解决方案1】:

我遇到了同样的问题并使用这种方式修复:

      @IBAction func btnDownload_touchUpInside(_ sender: UIButton) {

        YTVimeoExtractor.shared().fetchVideo(withVimeoURL: self.videoURL.text!, withReferer: nil, completionHandler: {(video, error) -> Void in

            if video != nil {

                self.videoTitle.text = video?.title


                if let streamUrls = video?.streamURLs
                {
                    var streamURL: String?

                    var streams : [String:String] = [:]
                    for (key,value) in streamUrls {
                        streams["\(key)"] = "\(value)"
                        print("\(key) || \(value)")
                    }
                    if let large = streams["720"]
                    {
                        streamURL = large
                    }
                    else if let high = streams["480"]
                    {
                        streamURL = high
                    }
                    else if let medium = streams["360"]
                    {
                        streamURL = medium
                    }
                    else if let low = streams["270"]
                    {
                        streamURL = low
                    }

                    if let url = streamURL
                    {
                        Alamofire.download(url, to: { (temporaryURL, response) -> (destinationURL: URL, options: DownloadRequest.DownloadOptions) in
                            if let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as? NSURL {

                                let fileName = response.suggestedFilename!
                                let finalPath = directoryURL.appendingPathComponent(fileName)
                                self.downloadedVideoPath = finalPath?.absoluteString
                                return (finalPath!, DownloadRequest.DownloadOptions(rawValue: 2))

                            }
                        }).downloadProgress(closure: { (progress) in
                            print("Progress: \(progress.fractionCompleted)")
                        })

                    }
                }
            }
        })
    }

    @IBAction func btnPlayOffLine_touchUpInside(_ sender: UIButton) {
        YTVimeoExtractor.shared().fetchVideo(withVimeoURL: self.videoURL.text!, withReferer: nil, completionHandler: {(video, error) -> Void in

            if video != nil {

                let videoURL = NSURL(string: self.downloadedVideoPath!)
                let player = AVPlayer(url: videoURL! as URL)
                let playerViewController = AVPlayerViewController()
                playerViewController.player = player
                self.present(playerViewController, animated: true) {
                    playerViewController.player!.play()

                }
            }
        })
    }

    @IBAction func btnPlayOnLine_touchUpInside(_ sender: UIButton) {


        YTVimeoExtractor.shared().fetchVideo(withVimeoURL: self.videoURL.text!, withReferer: nil, completionHandler: {(video, error) -> Void in

            if video != nil {

                self.videoTitle.text = video?.title


                if let streamUrls = video?.streamURLs
                {
                    var streamURL: String?

                    var streams : [String:String] = [:]
                    for (key,value) in streamUrls {
                        streams["\(key)"] = "\(value)"
                        print("\(key) || \(value)")
                    }
                    if let large = streams["720"]
                    {
                        streamURL = large
                    }
                    else if let high = streams["480"]
                    {
                        streamURL = high
                    }
                    else if let medium = streams["360"]
                    {
                        streamURL = medium
                    }
                    else if let low = streams["270"]
                    {
                        streamURL = low
                    }

                    if let url = streamURL
                    {
                        let videoURL = NSURL(string: url)
                        let player = AVPlayer(url: videoURL! as URL)
                        let playerViewController = AVPlayerViewController()
                        playerViewController.player = player
                        self.present(playerViewController, animated: true) {
                            playerViewController.player!.play()
                        }
                    }
                }
            }
        })
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多