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.导入本地文件

相关推荐
天桥吴彦祖10 小时前
判断iOS如何监听手机屏幕是否锁屏
ios
RTC实战笔记12 小时前
实时互动数字人怎么做,才不是一个只会说话的视频?
音视频·数字人·rtc·数字人接入
敲代码的鱼1 天前
PDF 预览与签名批注写回 支持安卓 iOS 鸿蒙 UTS插件
android·前端·ios
时光足迹1 天前
uni-app 视频通话实战:康复师与患者视频问诊的 6 个致命 Bug 与解决方案
android·ios·uni-app
时光足迹1 天前
JPush UniApp UTS 插件完全参考手册:API、事件与厂商通道一网打尽
vue.js·ios·uni-app
时光足迹1 天前
极光推送全攻略(下):uni-app 代码实现与 iOS 排查实战
vue.js·ios·uni-app
时光足迹1 天前
极光推送全攻略(上):被iOS证书折磨了三天,我写了一份前端也能看懂的避坑指南
前端·ios·uni-app
编程范式3 天前
SwiftUI 中图片如何适配可用空间
ios
songgeb5 天前
启发式 UI 自动化:从线性剧本到每步读屏决策
ios·测试
壹方秘境8 天前
我用Go语言开发了一个跨平台的HTTPS抓包和调试工具
前端·后端·ios