背景视频Objective-c
问题描述:
我想在我的视图中播放循环背景视频。我写了这个代码:背景视频Objective-c
-(AVPlayerLayer*)playerLayer{
if(!_playerLayer){
// find movie file
NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"fond_login" ofType:@"mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:[[AVPlayer alloc]initWithURL:movieURL]];
_playerLayer.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
[_playerLayer.player play];
return _playerLayer;
}
return nil; }
-(void)replayMovie:(NSNotification *)notification {
NSLog(@"Finis");
[self.playerLayer.player play];}
在我viewDidLoad中,我有:
[self.view.layer addSublayer:self.playerLayer];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(replayMovie:)
name: AVPlayerItemDidPlayToEndTimeNotification
object:nil];
我的问题是,视频不循环播放。我一次看到视频并完成。我不知道为什么..
感谢的
编辑:
我看到另一个教程。
@import MediaPlayer;
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
我在viewDidLoad中代码:
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"my_video" withExtension:@"format"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
self.moviePlayer.view.frame = self.view.frame;[self.view insertSubview:self.moviePlayer.view atIndex:0];
[self.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loopVideo) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
和:
- (void)loopVideo {
[self.moviePlayer play]; }
它的工作完美。
答
在你replayMovie:
方法,你需要寻求回到开头:
- (void)replayMovie:(NSNotification *)notification {
[(AVPlayerItem *)notification.object seekToTime:kCMTimeZero];
}
您还打算要设置的AVPlayer
的actionAtItemEnd
因此不会暂停:
_playerLayer.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
+0
感谢您的回答! –
你有没有看到你的replayMovie事件火灾? – Dan
没有replayMovie被称为.. –