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

相关推荐
叽哥3 小时前
Flutter Riverpod上手指南
android·flutter·ios
用户091 天前
SwiftUI Charts 函数绘图完全指南
ios·swiftui·swift
YungFan1 天前
iOS26适配指南之UIColor
ios·swift
权咚2 天前
阿权的开发经验小集
git·ios·xcode
用户092 天前
TipKit与CloudKit同步完全指南
ios·swift
法的空间2 天前
Flutter JsonToDart 支持 JsonSchema
android·flutter·ios
kaixin_啊啊2 天前
突破限制:Melody远程音频管理新体验
音视频
2501_915918412 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
ai产品老杨2 天前
解锁仓储智能调度、运输路径优化、数据实时追踪,全功能降本提效的智慧物流开源了
javascript·人工智能·开源·音视频·能源
MThinker2 天前
02-Media-8-uvc_with_csc.py 使用硬件解码的USB摄像头(UVC)捕获视频并显示的程序
音视频·智能硬件·micropython·canmv·k230