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

相关推荐
美狐美颜SDK开放平台1 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
2601_955662461 天前
短剧多人对白怎么配?2026年多角色配音工具对比
大数据·人工智能·音视频·语音识别
开发笔记-阿牛2 天前
蓝牙音乐灯拆解,又一次拆到蓝牙音乐灯芯片CK6865L
人工智能·单片机·嵌入式硬件·音视频·音频
茶底世界之下2 天前
为什么预览、录制、离线导出不能共享同一个背压策略?
ios·swift
2501_916007472 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
旺仔小馒头wang2 天前
AI 智能手机发布会:赋予人类的五种新能力
人工智能·学习·音视频
纳祥科技2 天前
拆机拆到芯片级:ARC功放机里的NX7026、NX8420、NX6805
单片机·音视频·智能音箱
youandyouyou2 天前
律所内部培训共享的内网部署方案:架构、参数与三步落地
音视频
2501_916008892 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
2501_915909062 天前
移动端开发工具链怎么组合比较好,iOS、Android、跨端三套配置方案
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程