gpt4 book ai didi

swift - 在 IOS 中使用 Swift 播放 MP4

转载 作者:行者123 更新时间:2023-11-30 11:34:55 38 4
gpt4 key购买 nike

这似乎是一项简单的任务,但我再次发现自己不知所措。这是代码。它返回“找到视频”,但只显示白屏。帮助!:

import Foundation
import UIKit
import AVFoundation
import MediaPlayer
import AVKit

class SampleView: UIViewController {
var sw: CGFloat = 0;
var sh: CGFloat = 0;
var hw: CGFloat = 0;

override func viewDidLoad() {
super.viewDidLoad()
if let filePath = Bundle.main.path(forResource: theImage, ofType: "mp4") {
print("found video")
let videoURL = URL(string: filePath)
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
} else{print("no found video")}

最佳答案

您需要将代码移至 viewDidAppear,因为 viewDidLoad View 的层次结构尚未完成,无法呈现任何 VC

override func viewDidAppear(_ animated:Bool) {
super.viewDidAppear(animated)
if let filePath = Bundle.main.path(forResource: theImage, ofType: "mp4") {
print("found video")
let videoURL = URL(string: filePath)
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
} else{print("no found video")
}

关于swift - 在 IOS 中使用 Swift 播放 MP4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49867640/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com