AVPlayer 无法正常工作 - Swift
Posted
技术标签:
【中文标题】AVPlayer 无法正常工作 - Swift【英文标题】:AVPlayer not working properly - Swift 【发布时间】:2016-07-15 16:07:13 【问题描述】://首先我的英语很差,对不起... 我正在实现 AVPlayer 我的应用程序。播放器工作正常,但如果我多次播放其他视频,播放器无法正常工作,只有this screen 看起来。
这是我的代码
let player:AVPlayer?
override func viewDidLoad()
super.viewDidLoad()
let videoFile = object?.objectForKey("video") as? PFFile
videoFile?.getFilePathInBackgroundWithBlock( (filePath, error) in
if error == nil
let videoURL = NSURL(fileURLWithPath: filePath!)
self.player = AVPlayer(URL: videoURL)
let playerController = AVPlayerViewController()
playerController.player = self.player
playerController.showsPlaybackControls = false
playerController.videoGravity = AVLayerVideoGravityResizeAspectFill
playerController.view.frame = self.videoView.bounds
self.videoView.addSubview(playerController.view)
self.addChildViewController(playerController)
self.player!.play()
else
print(error)
)
【问题讨论】:
【参考方案1】:我不明白您获取视频对象的方式,但您可以尝试以下代码。它工作正常。
var player:AVPlayer! // this shouldn't be let.
override func viewDidLoad()
super.viewDidLoad()
let query = PFQuery(className:"TestClass") //your classname in parse. Change it with your clas name
query.getObjectInBackgroundWithId("SALe4gX2nk") // get the video file with id. Change it with your id.
(object: PFObject?, error: NSError?) -> Void in
let videoFile = object?.objectForKey("video") as? PFFile
let url = NSURL(string: (videoFile?.url)!)
self.player = AVPlayer(URL: url!)
let playerController = AVPlayerViewController()
playerController.player = self.player
playerController.showsPlaybackControls = false
playerController.videoGravity = AVLayerVideoGravityResizeAspectFill
playerController.view.frame = self.view.bounds
self.view.addSubview(playerController.view)
self.addChildViewController(playerController)
self.player!.play()
【讨论】:
我正在获取 PFQueryTableViewController。此控制器详细视图控制器。问题不在于获取。 AVPlayer 已经可以正常工作并播放视频了。我的问题是我多次播放视频,一段时间后 AVPlayer 无法正常工作。 检查this以上是关于AVPlayer 无法正常工作 - Swift的主要内容,如果未能解决你的问题,请参考以下文章
AVPlayer 状态 AVPlayerStatusFailed 无法正常工作