ios 如何在没有控件的 UIView 中播放视频,如背景/壁纸?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35226532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play video inside a UIView without controls, like a background/wallpaper?
提问by Bogdan Laukhin
The goal is to playback video file (*.mp4) inside a UIView without controls.
目标是在没有控件的 UIView 中播放视频文件 (*.mp4)。
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.
它将作为 ViewController 和其他控件上的背景/墙纸,即 tableview、文本字段、图像将显示在视频播放的视图上。
What is the better way to do this? Thank you
什么是更好的方法来做到这一点?谢谢
回答by Bogdan Laukhin
I've reached the goal with the native AVPlayer
我已经达到了与本地人的目标 AVPlayer
1.Used AVFoundation:
1.二手AVFoundation:
#import <AVFoundation/AVFoundation.h>
2.Used property for player:
2.玩家使用的属性:
@property (nonatomic) AVPlayer *avPlayer;
3.Added video file into "Video" folder and added "Video" into project
3.将视频文件添加到“视频”文件夹中,并将“视频”添加到项目中
4.Initialized the player
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.Subscribed for event - video did play to the end
5.订阅事件 - 视频确实播放到最后
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avPlayer currentItem]];
6.Resumed video playing from the very start in related method
6.在相关方法中从一开始就恢复视频播放
- (void)itemDidFinishPlaying:(NSNotification *)notification {
AVPlayerItem *player = [notification object];
[player seekToTime:kCMTimeZero];
}
回答by Suragch
Swift
迅速
In Swift it is similar. Add the video to your resource bundle. My fuller answer is here.
在 Swift 中也是类似的。将视频添加到您的资源包。我更完整的答案在这里。
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVPlayer?
@IBOutlet weak var videoViewContainer: UIView!
override func viewDidLoad() {
super.viewDidLoad()
initializeVideoPlayerWithVideo()
}
func initializeVideoPlayerWithVideo() {
// get the path string for the video from assets
let videoString:String? = Bundle.main.path(forResource: "SampleVideo_360x240_1mb", ofType: "mp4")
guard let unwrappedVideoPath = videoString else {return}
// convert the path string to a url
let videoUrl = URL(fileURLWithPath: unwrappedVideoPath)
// initialize the video player with the url
self.player = AVPlayer(url: videoUrl)
// create a video layer for the player
let layer: AVPlayerLayer = AVPlayerLayer(player: player)
// make the layer the same size as the container view
layer.frame = videoViewContainer.bounds
// make the video fill the layer as much as possible while keeping its aspect size
layer.videoGravity = AVLayerVideoGravity.resizeAspectFill
// add the layer to the container view
videoViewContainer.layer.addSublayer(layer)
}
@IBAction func playVideoButtonTapped(_ sender: UIButton) {
// play the video if the player is initialized
player?.play()
}
}