如何在iOS中播放循环视频?

时间:2016-09-09 16:21:02

标签: ios swift video avplayer

如何在我的故事板中放置视频片段?我只看到了:

enter image description here

另外,我在哪里放.mp4文件?

enter image description here

最后,当View Controller启动时,包含循环视频剪辑的正确代码是什么。

 //insert looping video clip here

我熟悉Android Studio / java,可以做到这一点没问题。但是,我对swift和Xcode很新,所以我遇到了麻烦。

2 个答案:

答案 0 :(得分:6)

制作循环视频: -

  • 向ViewController添加urldecode,相应地设置约束。

  • 在您的合规班级中将eval声明为UIView

    UIView
  • 为视频播放器@IBOutlet创建自定义类: @IBOutlet weak var videoView : VideoPlay! //Where VideoPlay is a CustomClass for the Video player

    UIVew
  • 修改符合ViewController的StoryBoard: -

    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() } } } 符合 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 的全局功能。

至于保存视频文件的位置,请将其保存在主包中,即: - 在您的情况下 Fighting Trainer Pro 文件夹

如: -

<强> VideoPlay

<强> VideoPlay

答案 1 :(得分:0)

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