ios - 使用 Avplayer/kit 问题播放本地视频

标签 ios swift xcode avplayer

每当我运行该项目时,它都会调出视频播放器,但视频不播放  (the code and error) . 我该如何解决?如果图片中的代码不够清楚,这里是代码:

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 - Window on drag and drop

在将其添加回来时确保您 -

Check the projectTarget checkbox like the screenshot below -

Checked box for app target

确保文件名为small.mp4

关于ios - 使用 Avplayer/kit 问题播放本地视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42377834/

相关文章:

ios - 使用 IAM 用户凭证 iOS 的 AWS iOT 登录问题

ios - 无法正确设置 Sprite 的速度

ios - Swift - 不能使 scrollview 能够向左滚动吗?

当文件名在 XCode 3.1.2 中保持不变时图像不会更新

ios - 在哪里保存以编程方式创建的文件,以保护它们免受更新?

ios - 将 block 存储在参数数量不断变化的变量中

android - SQLite 在 Android 和 iOS 上的性能差异

ios - 快速获取滑动手势的方向

swift - 更新到iOS 13后指纹认证问题

objective-c - 在 Objective-C 中使用带有可为空字符串值的 Swift 完成 block