如何在 iOS 中播放循环视频?
Posted
技术标签:
【中文标题】如何在 iOS 中播放循环视频?【英文标题】:How to play a looping video in iOS? 【发布时间】:2016-09-09 16:21:02 【问题描述】:如何将视频剪辑放入我的故事板?我看到的是:
另外,我应该把 .mp4 文件放在哪里?
最后,当 View Controller 启动时,循环视频剪辑包含的正确代码是什么。
//insert looping video clip here
我熟悉 android Studio/java 并且可以毫无问题地做到这一点。但是,我对 swift 和 Xcode 很陌生,所以我遇到了麻烦。
【问题讨论】:
【参考方案1】:制作循环视频:-
将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()
【讨论】:
我需要将 mp4 文件放在任何地方吗?另外,我需要将任何东西拖到我的故事板上吗? 只是一个循环播放的视频片段,我不知道循环播放的背景视频/和循环播放的视频有什么区别。 绝对没有区别,但可以在 videoPlayer 上添加其他子视图,仅此而已......我已经更新了我的答案:) 在符合storyboard viewcontroller的viewController类的类函数viewDidLoad()
中。我在回答中已经提到了...
Apple 在名为 Advances in AVFoundation Playback 的 WWDC 2016 会议期间提供了一些示例代码 (AVPlayerLooper)。他们讨论了使用 AVQueuePlayer
幻灯片 59:devstreaming.apple.com/videos/wwdc/2016/503lkp4vimgv7tqecn7/503/… 示例代码:developer.apple.com/library/prerelease/content/samplecode/… 循环播放视频的最佳方式【参考方案2】:
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
【讨论】:
以上是关于如何在 iOS 中播放循环视频?的主要内容,如果未能解决你的问题,请参考以下文章