如何在 iOS 中播放循环视频?
How to play a looping video in iOS?
如何将视频剪辑放入故事板?我只看到:
另外,.mp4 文件应该放在哪里?
最后,当视图控制器启动时,为循环视频剪辑包含的正确代码是什么。
//insert looping video clip here
我熟悉 Android Studio/java 并且可以毫无问题地做到这一点。但是,我对 swift 和 Xcode 很陌生,所以我遇到了麻烦。
制作循环视频:-
将 UIView
添加到您的 ViewController,相应地设置约束。
在符合要求的 class
中将 UIView
声明为 @IBOutlet
@IBOutlet weak var videoView : VideoPlay!
//Where VideoPlay is a CustomClass for the Video player
为视频播放器创建自定义 Class 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()
}
}
}
根据 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
符合classVideoPlay
,可以访问
VideoPlay
的全局函数。
至于保存视频文件的位置,请将其保存在主包中,即:- 在您的情况下 Fighting Trainer Pro 文件夹
例如:-
toggleMute()
isMuted()
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
如何将视频剪辑放入故事板?我只看到:
另外,.mp4 文件应该放在哪里?
最后,当视图控制器启动时,为循环视频剪辑包含的正确代码是什么。
//insert looping video clip here
我熟悉 Android Studio/java 并且可以毫无问题地做到这一点。但是,我对 swift 和 Xcode 很陌生,所以我遇到了麻烦。
制作循环视频:-
将
UIView
添加到您的 ViewController,相应地设置约束。在符合要求的 class
中将UIView
声明为@IBOutlet
@IBOutlet weak var videoView : VideoPlay! //Where VideoPlay is a CustomClass for the Video player
为视频播放器创建自定义 Class
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() } } }
根据 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
符合classVideoPlay
,可以访问VideoPlay
的全局函数。
至于保存视频文件的位置,请将其保存在主包中,即:- 在您的情况下 Fighting Trainer Pro 文件夹
例如:-
toggleMute()
isMuted()
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