Swift3 使用 AVPlayer 播放多个视频文件



我有一个小代码,可以让我在点击UIButton时播放本地文件。但我想要的是 3 个不同的 UIButton 上播放多个文件,因为我有 3 个视频文件要附加到我的应用程序。

这是当前代码:

import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
var playerController = AVPlayerViewController()
var player:AVPlayer?

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let videoString:String? = Bundle.main.path(forResource: "Video", ofType: ".mp4")
if let url = videoString {
let videoURL = NSURL(fileURLWithPath: url)
self.player = AVPlayer(url: videoURL as URL)
self.playerController.player = self.player

}

}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func PlayVideo(_ sender: AnyObject) {
self.present(self.playerController, animated: true, completion: {
self.playerController.player?.play()
})
}

}

您可以在按钮单击功能中为不同的视频文件创建网址

@IBAction func Button1Click(_ sender: AnyObject) {
let videoString:String? = Bundle.main.path(forResource: "Video1", ofType: ".mp4")
if let url = videoString {
let videoURL = NSURL(fileURLWithPath: url)
self.player = AVPlayer(url: videoURL as URL)
self.playerController.player = self.player
}
self.present(self.playerController, animated: true, completion: {
self.playerController.player?.play()
})
}
@IBAction func Button2Click(_ sender: AnyObject) {
let videoString:String? = Bundle.main.path(forResource: "Video2", ofType: ".mp4")
if let url = videoString {
let videoURL = NSURL(fileURLWithPath: url)
self.player = AVPlayer(url: videoURL as URL)
self.playerController.player = self.player
}
self.present(self.playerController, animated: true, completion: {
self.playerController.player?.play()
})
}
@IBAction func Button3Click(_ sender: AnyObject) {
let videoString:String? = Bundle.main.path(forResource: "Video3", ofType: ".mp4")
if let url = videoString {
let videoURL = NSURL(fileURLWithPath: url)
self.player = AVPlayer(url: videoURL as URL)
self.playerController.player = self.player
}
self.present(self.playerController, animated: true, completion: {
self.playerController.player?.play()
})
}

相关内容

  • 没有找到相关文章

最新更新