iOS Swift5 视频播放

文章目录

1.截图

2.代码

swift 复制代码
import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        // 创建按钮
        let button = UIButton(type: .system)
        button.frame = CGRect(x: 100, y: 100, width: 200, height: 50)
        button.setTitle("Play Video", for: .normal)
        button.backgroundColor = .systemBlue
        button.setTitleColor(.white, for: .normal)
        
        // 添加按钮的点击事件
        button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        
        // 将按钮添加到视图中
        self.view.addSubview(button)
    }


    var playerViewController: AVPlayerViewController!
    var player: AVPlayer!


    @objc func buttonTapped() {
        //本地视频
        guard let localVideoURL = Bundle.main.url(forResource: "sample3", withExtension: "mp4") else {
            MyPrint("Invalid URL")
            return
        }
        // 创建AVPlayer
        player = AVPlayer(url: localVideoURL)

        // 创建AVPlayerViewController并设置player
        playerViewController = AVPlayerViewController()
        playerViewController.player = player
        present(playerViewController, animated: true) { [self] in
            player.play()
        }

    }

}

3.导入本地文件

相关推荐
martindelophy1 小时前
开源 AI 视频编辑器实战:用结构化命令连接视频生成、对话剪辑与浏览器导出
人工智能·编辑器·音视频
小π军1 小时前
安卓、iOS、鸿蒙三端的理解
android·ios·harmonyos
l14372332672 小时前
短剧视频翻译配音哪家好?批量多语种方案对比
音视频
常州晟凯电子科技2 小时前
8路CVBS/AHD合成一路视频无线传输系统评测
音视频·视频编解码
youandyouyou2 小时前
教师远程教研共享屏幕:技术拆解与落地实践
音视频·网络通信
Dovis(誓平步青云)2 小时前
一年1956亿行:智能电表场景下的时序数据存储实践
运维·前端·自动化·音视频·prometheus
终端安全笔记2 小时前
声明式之后,怎么证明更新策略真的生效了
ios·iphone
开开心心就好3 小时前
Windows视频播放器推荐,精简版体积更小
java·服务器·开发语言·eclipse·pdf·ocr·音视频
Sayai3 小时前
ComfyUI+MiniMax H3 图生视频实战:一张静态图 5 分钟变带音效大片(12G 显存,附工作流)
人工智能·aigc·音视频·comfyui·图生视频·minimax
茶底世界之下3 小时前
瀑布的白色需要压住吗?用 Awaking 检查高光调整的边界
ios