文章目录
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()
}
}
}