每当我运行该项目时,它都会调出视频播放器,但视频不播放 . 我该如何解决?如果图片中的代码不够清楚,这里是代码:
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() }
}
最佳答案
评论中提出的解决方案和@Guan 回答的都是正确的。
你的urlPathString
在这里实际上是nil,
让它工作——
1- Either add the Target Membership, as suggested in the comment and the answer above or
执行以下操作
1.1- Delete the small.mp4 from bundle, make sure you
move it to trash
2.1 - Go to trash and drag and drop your mp4 file into the project again, you will get a window like this -
在将其添加回来时确保您 -
Check the projectTarget checkbox like the screenshot below -
确保文件名为small.mp4
关于ios - 使用 Avplayer/kit 问题播放本地视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42377834/