【发布时间】:2016-09-09 16:21:02
【问题描述】:
如何将视频剪辑放入我的故事板?我看到的是:
另外,我应该把 .mp4 文件放在哪里?
最后,当 View Controller 启动时,循环视频剪辑包含的正确代码是什么。
//insert looping video clip here
我熟悉 Android Studio/java 并且可以毫无问题地做到这一点。但是,我对 swift 和 Xcode 很陌生,所以我遇到了麻烦。
【问题讨论】:
如何将视频剪辑放入我的故事板?我看到的是:
另外,我应该把 .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()
【讨论】:
viewDidLoad()中。我在回答中已经提到了...
AVQueuePlayer 幻灯片 59:devstreaming.apple.com/videos/wwdc/2016/503lkp4vimgv7tqecn7/503/… 示例代码:developer.apple.com/library/prerelease/content/samplecode/… 循环播放视频的最佳方式
Dravidian 答案的 Objective-C 版本。还包含关键的layoutSubviews 方法,使视频层实际上保持与视图相同的大小!
#import "LBVideoView.h"
#import <AVFoundation/AVFoundation.h>
@interface LBVideoView ()
{
AVPlayer *_player;
AVPlayerLayer *_playerLayer;
}
@end
@implementation LBVideoView
- (instancetype)initWithFrame:(CGRect)frame
{
return [[super initWithFrame:frame] commonInit];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
return [[super initWithCoder:aDecoder] commonInit];
}
- (instancetype)commonInit
{
_playerLayer = [AVPlayerLayer new];
_playerLayer.backgroundColor = UIColor.clearColor.CGColor;
_playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
[self.layer addSublayer:_playerLayer];
_playerLayer.frame = self.bounds;
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)playVideoWithURL:(NSURL*)url
{
_player = [AVPlayer playerWithURL:url];
_playerLayer.player = _player;
[_player play];
[self loopVideo];
}
- (void)loopVideo
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieEnded) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}
- (void)movieEnded
{
[_player seekToTime:kCMTimeZero];
[_player play];
}
- (void)layoutSubviews
{
[super layoutSubviews];
_playerLayer.frame = self.bounds;
}
@end
【讨论】: