gpt4 book ai didi

ios - 视频无法使用 swift 代码

转载 作者:行者123 更新时间:2023-11-28 08:25:00 25 4
gpt4 key购买 nike

所有这些代码功能都是 View Controller 上的一个按钮。该按钮应播放视频。现在代码中没有错误消息。所发生的只是按钮被按下而什么也没有发生。没有错误信息。我只有一个 View Controller 页面。下面列出了代码。

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {


var moviePlayer: AVPlayerViewController?

override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}


@IBAction func playVideo(_ sender: UIButton) {
playLocalVideo()
}
func playLocalVideo(){

if moviePlayer != nil {
do {
let filePath = Bundle.main.path(forResource: "t", ofType: "mp4")

let videoURL = NSURL(fileURLWithPath: filePath!)


let player = AVPlayer(url: videoURL as URL)
let playViewController = AVPlayerViewController()

playViewController.player = player
self.present(playViewController, animated: true) {() -> Void in playViewController.player!.play()

}

} catch {
print(error)
}}}}

最佳答案

试试下面的代码

 import UIKit
import AVKit
import AVFoundation


class ViewController: UIViewController {

let moviePlayer = AVPlayerViewController()


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
playLocalVideo()
}




func playLocalVideo(){

if let filePath = Bundle.main.path(forResource: "t", ofType: "mp4"){
let videoURL = NSURL(fileURLWithPath: filePath)


let player = AVPlayer(url: videoURL as URL)


moviePlayer.player = player
self.present(moviePlayer, animated: true) {
self.moviePlayer.player!.play()
}
}

else{
print("Oops, something wrong when playing video.mp4")
}
}


}

并确保在添加视频到目标时,它不应该是像下面这样的图像

enter image description here

它应该像下面的一样

enter image description here

关于ios - 视频无法使用 swift 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40297257/

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