本文介绍了如何在没有控件(例如背景/墙纸)的UIView中播放视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是在没有控件的UIView中播放视频文件(* .mp4).

The goal is to playback video file (*.mp4) inside a UIView without controls.

它将用作ViewController和其他控件的背景/墙纸,例如,表视图,文本字段,图像将在视图上显示并播放视频.

It will serve as a background/wallpaper on the ViewController and other controls, i.e. tableview, text fields, images will be shown over the view with video playedback.

什么是更好的方法?谢谢

What is the better way to do this?Thank you

推荐答案

我已经达到了使用本地AVPlayer

I've reached the goal with the native AVPlayer

1.使用的AVFoundation:

1.Used AVFoundation:

#import <AVFoundation/AVFoundation.h>

2.玩家已使用的属性:

2.Used property for player:

@property (nonatomic) AVPlayer *avPlayer;

3.将视频文件添加到视频"文件夹中,并将视频"添加到项目中

3.Added video file into "Video" folder and added "Video" into project

4.初始化播放器

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"shutterstock_v885172.mp4" ofType:nil inDirectory:@"Video"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.view.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];

[self.avPlayer play];

5.订阅活动-视频播放到最后

5.Subscribed for event - video did play to the end

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avPlayer currentItem]]; 

6.从一开始就以相关方法恢复播放视频

6.Resumed video playing from the very start in related method

- (void)itemDidFinishPlaying:(NSNotification *)notification {
    AVPlayerItem *player = [notification object];
    [player seekToTime:kCMTimeZero];
}

这篇关于如何在没有控件(例如背景/墙纸)的UIView中播放视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 23:25