作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
如何将视频剪辑放入 Storyboard?我只看到:
另外,我应该把 .mp4 文件放在哪里?
最后,当 View Controller 启动时,循环视频剪辑的正确代码是什么。
//insert looping video clip here
我熟悉 Android Studio/java,可以毫无问题地做到这一点。但是,我对 swift 和 Xcode 还很陌生,所以遇到了麻烦。
最佳答案
制作循环视频:-
将 UIView
添加到您的 ViewController,相应地设置约束。
在您的符合类中将 UIView
声明为 @IBOutlet
@IBOutlet weak var videoView : VideoPlay!
//Where VideoPlay is a CustomClass for the Video player
为视频播放器 UIVew
创建自定义类:VideoPlay
import UIKit
import AVFoundation
class VideoPlay: UIView {
private var player : AVPlayer!
private var playerLayer : AVPlayerLayer!
init() {
super.init(frame: CGRectZero)
self.initializePlayerLayer()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.initializePlayerLayer()
self.autoresizesSubviews = false
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initializePlayerLayer()
}
private func initializePlayerLayer() {
playerLayer = AVPlayerLayer()
playerLayer.backgroundColor = UIColor.whiteColor().CGColor
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.bounds
}
func playVideoWithURL(url: NSURL) {
player = AVPlayer(URL: url)
player.muted = false
playerLayer.player = player
player.play()
loopVideo(player)
}
func toggleMute() {
player.muted = !player.muted
}
func isMuted() -> Bool
{
return player.muted
}
func loopVideo(videoPlayer: AVPlayer) {
NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification, object: nil, queue: nil) { notification in
videoPlayer.seekToTime(kCMTimeZero)
videoPlayer.play()
}
}
}
修改您的 StoryBoard 符合 ViewController :-
class ViewController: UIViewController {
@IBOutlet weak var videoView : VideoPlay!
override func viewDidLoad() {
super.viewDidLoad()
let bundle: NSBundle = NSBundle.mainBundle()
let moviePath: String = bundle.pathForResource("yourVideoFile_Name", ofType: "yourVideoFile_Type")!
let movieUrl : NSURL = NSURL.fileURLWithPath(moviePath)
videoView.playVideoWithURL(movieUrl)
}....
}
由于 videoView
符合类 VideoPlay
,您可以访问VideoPlay
的全局函数。
至于保存视频文件的位置,请将其保存在主包中,即:- 在您的情况下,Fighting Trainer Pro 文件夹
例如:-
toggleMute()
isMuted()
关于ios - 如何在 iOS 中播放循环视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39415854/
我是一名优秀的程序员,十分优秀!