每当我运行该项目时,它都会启动视频播放器,但视频无法播放 。 我该如何解决?如果图片中的代码不够清晰,则代码如下:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
let avPlayerViewController = AVPlayerViewController()
var avPlayer:AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from nib.
let urlPathString = Bundle.main.path(forResource: "small", ofType: "mp4")
if urlPathString != nil {
let movieUrl = NSURL(fileURLWithPath: "/Users/kevingerman/Desktop/Pioneer News Season 2 Episode 1.mp4")
self.avPlayer = AVPlayer(url: movieUrl as URL)
self.avPlayerViewController.player = self.avPlayer
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func playButtonTapped(_ sender: AnyObject) {
//Trigger the video to play
self.present(self.avPlayerViewController, animated: true) { () -> Void in self.avPlayerViewController.player?.play() }
}
}
答案 0 :(得分:1)
评论中提出的解决方案和@Guan回答的解决方案都是正确的。
你的urlPathString
实际上是零,
使其有效 -
1-按照评论和上面的答案或
的建议添加目标会员资格
执行以下操作
1.1-从包中删除small.mp4,确保
move it to trash
在添加它的同时确保你 -
检查projectTarget复选框,如下面的屏幕截图所示 -
确保文件名为small.mp4
答案 1 :(得分:0)
确保您的urlPathString
不是零。
我不知道下面的代码是什么意思:
let movieUrl = NSURL(fileURLWithPath: "/Users/kevingerman/Desktop/Pioneer News Season 2 Episode 1.mp4")
我认为模拟器无法获取mac中的文件路径。(我不确定,我之前没有这样做)。 您可以检查文件的目标成员资格。
代码可以这样工作:
var urlPaht = Bundle.main.url(forResource: "small", withExtension:"mp4")
if urlPaht == nil {
urlPaht = NSURL(fileURLWithPath: "/Users/kevingerman/Desktop/Pioneer News Season 2 Episode 1.mp4")
}
self.avPlayer = AVPlayer(url: movieUrl as URL)
self.avPlayerViewController.player = self.avPlayer