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

相关推荐
Anhty1 小时前
手机端叮咚变声器2026实测:功能表现、优缺点完整解析
功能测试·ios·智能手机·安卓
SameX4 小时前
后台定位耗电的问题,我花两年把它压到了3%-5%
ios
林澈在路上5 小时前
可以用自己声音写歌的软件哪款好用 实测对比
大数据·人工智能·aigc·音视频·音频
程序员-李俞5 小时前
MiniMax H3 深度拆解:全模态视频模型来了,AI 漫剧系统该如何重构?
人工智能·ai作画·重构·aigc·音视频·ai写作·画布
淡淡的香烟5 小时前
Android智能猫砂盆视频加载慢卡顿问题分析
android·服务器·音视频
小猴子爱上树6 小时前
跨马翻译:批量图片翻译与视频字幕工具,跨境电商高效助手
python·音视频
山顶夕景6 小时前
【全模态】音视频理解模型Audio-Visual Flamingo
音视频·video·vlm·多模态理解
郑午时光17 小时前
全球首发!2026年适合IP短剧长视频的AI视频生成工具,即梦seedance2.5轻松做短剧
人工智能·tcp/ip·音视频
星辰即远方17 小时前
计算器仿写总结
学习·ui·ios·cocoa·xcode
游戏开发爱好者820 小时前
系统状态:专为iPhone和iPad设计的实时系统性能监控工具全面介绍
android·ios·小程序·uni-app·iphone·webview·ipad