gpt4 book ai didi

swift - 当我尝试使用此 YTVimeoExtractor 时,AVPlayer 无法工作

转载 作者:行者123 更新时间:2023-11-30 10:03:50 25 4
gpt4 key购买 nike

当我尝试通过 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.
}
}

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

enter image description here

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

最佳答案

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

      @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()
}
}
}
}
})
}
}

关于swift - 当我尝试使用此 YTVimeoExtractor 时,AVPlayer 无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37167266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com