我想在发布屏幕上放置一个MP4视频。就像在启动画面上加载图像但现在只是一个视频的人一样。而且我确实知道如何在启动画面上放置图像而不是视频任何帮助?
答案 0 :(得分:7)
您可以为“主界面”创建自定义ViewController,并在启动屏幕之后使用它,并在其中使用AVPlayer。在swift中会是这样的:
var player: AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
loadVideo()
}
private func loadVideo() {
//this line is important to prevent background music stop
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
} catch { }
let path = NSBundle.mainBundle().pathForResource("your path", ofType:"mp4")
player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
playerLayer.zPosition = -1
self.view.layer.addSublayer(playerLayer)
player?.seekToTime(kCMTimeZero)
player?.play()
}
答案 1 :(得分:1)
使用swift3中的最新语法
import MediaPlayer
class ViewController: UIViewController {
var player: AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
self.loadVideo()
// Do any additional setup after loading the view, typically from a nib.
}
private func loadVideo() {
//this line is important to prevent background music stop
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
} catch { }
let path = Bundle.main.path(forResource: "Juvi Landing Page Animation", ofType:"mp4")
let filePathURL = NSURL.fileURL(withPath: path!)
let player = AVPlayer(url: filePathURL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
playerLayer.zPosition = -1
self.view.layer.addSublayer(playerLayer)
player.seek(to: kCMTimeZero)
player.play()
}
答案 2 :(得分:1)
Swift 5.1
您可以为“主界面”创建自定义ViewController,并在LaunchScreen之后使用内部使用AVPlayer的方式使用它。很快就会是这样的:
import AVFoundation
import AVKit
var player = AVPlayer()
private func loadVideo() {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient)
} catch { }
let path = Bundle.main.path(forResource: "Video Path", ofType:"mp4")
player = AVPlayer(url: NSURL(fileURLWithPath: path!) as URL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
playerLayer.zPosition = -1
self.view.layer.addSublayer(playerLayer)
player.seek(to: CMTime.zero)
player.play()
}
答案 3 :(得分:0)
您无法在启动屏幕上显示视频,但可以在苹果启动后立即启动视频。 在视觉上,效果可以无法区分。
只需在第一个视图控制器的 viewDidLoad 方法上添加视频播放调用,并置于某个控件中,不要强制用户在应用运行时始终查看它。否则,您的用户很快就会生气。
Yom可能希望将视频的第一张照片调整到您的启动屏幕,并为多种设备分辨率提供不同的视频。请注意,各种移动苹果设备支持的各种视频尺寸并不总是与每个设备的显示尺寸相匹配。
您的里程可能会有所不同,您需要"旋转"你的视频看起来像是"垂直"在横向播放时。
E.g:
- (void) play{
NSString *videoName=@"video320";
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
videoName=@"vid_trailer";
CGFloat height =self.view.frame.size.height;
//test retina display
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) {
// RETINA DISPLAY - you may need to add check for @3x resolutions
videoName=@"video920";
}
if (height > 460.0){
videoName=@"video1096";
}
} else {
videoName=@"video768";
self.view.backgroundColor=[UIColor blackColor];
}
NSString *urlString = [[NSBundle mainBundle] pathForResource:videoName ofType:@"mp4"];
NSLog(@"urlString=%@",urlString);
NSURL *url = [NSURL fileURLWithPath:urlString];
if (self.videoPlayer != nil)
{
[self.videoPlayer stop];
self.videoPlayer=nil;
}
self.videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.videoPlayer.view.frame=self.view.frame;
self.videoPlayer.fullscreen=NO;
[self.videoPlayer setScalingMode:MPMovieScalingModeAspectFill];
[self.videoPlayer setShouldAutoplay:YES];
[self.videoPlayer setControlStyle:MPMovieControlStyleNone];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(terminateVideo:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
for(UIView* subV in self.videoPlayer.view.subviews) {
subV.backgroundColor = [UIColor clearColor];
}
[self.tabBarController.view addSubview:self.videoPlayer.view];
[self.navigationController.view addSubview:self.videoPlayer.view];
[self.videoPlayer play];
}
- (void) terminateVideo:(id)sender{
[self.videoPlayer.view removeFromSuperview];
self.videoPlayer =nil;
self.view.backgroundColor=[UIColor darkGrayColor];
}