【发布时间】:2016-10-21 08:51:38
【问题描述】:
我有一个splash_screen.mp4 文件。
这是我在.h 文件中的代码
@property AVPlayer * player;
@property AVPlayerLayer * playerLayer;
在.m 文件中:
NSURL* mURL = [[NSBundle mainBundle] URLForResource:@"splash_screen" withExtension:@"mp4"];
self.player = [AVPlayer playerWithURL:mURL];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
Float64 seconds = 1.5f;
CMTime targetTime = CMTimeMakeWithSeconds(seconds, NSEC_PER_SEC);
[self.player seekToTime:targetTime];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.player currentItem]];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.playerLayer.needsDisplayOnBoundsChange = YES;
[self.playerLayer setFrame:[self.view bounds]];
[self.view.layer addSublayer:self.playerLayer];
self.view.layer.needsDisplayOnBoundsChange = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
if (self.player) {
[self.player play];
}
});
以前我有一个.mov文件,可以播放视频文件,但现在我有一个.mp4文件,不能播放。
在模拟器上测试,视频可以运行,但在设备上,它没有运行。
【问题讨论】:
-
它对我有用。当您播放 .mp4 文件时,应用程序是否崩溃?如果它崩溃了,控制台的输出是什么?
-
@ronan,它不会崩溃。只是白色的空白屏幕。调试了一下,可以得到url,但是不知道为什么视频不能播放。
-
检查,它已正确复制到副本包资源中。
-
尝试另一个低分辨率的 mp4 文件,我认为它也可以在 5 和 5 秒内工作
标签: ios objective-c video