0

当我尝试通过 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 one 来做这个示例项目。你可以试一试,然后告诉我。

4

1 回答 1

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()
                        }
                    }
                }
            }
        })
    }
}
于 2017-07-20T19:41:17.307 回答