3

AVFoundation用来实现一个AVPlayer. 我想连续循环一个视频剪辑,所以我注册一个AVPlayerItemDidPlayToEndTimeNotification来调用这个方法:

- (void)player1ItemDidReachEnd:(NSNotification *)notification
{ 
 dispatch_async(dispatch_get_main_queue(),
       ^{
        [player1 seekToTime:kCMTimeZero]; 
        [player1 play];
       });
}

它有时会起作用,但最终会停止播放,大概是因为seekToTime. 如何使此代码防弹?

4

4 回答 4

5
player.actionAtItemEnd = AVPlayerActionAtItemEndNone; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification object:[player currentItem]];

(void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
 [p seekToTime:kCMTimeZero];
}   
于 2011-05-02T10:51:55.253 回答
5

我现在通过将 AVPlayer 的 AVPlayerActionAtItemEnd 属性设置为 AVPlayerActionAtItemEndNone 来解决此问题 - 默认值为 AVPlayerActionAtItemEndPause。AVPlayer 不再在剪辑结束时暂停,因此不必重新开始播放。还发现没有必要将 seekToTime 分派到主队列。

于 2010-11-01T04:53:02.177 回答
0
- (void)playLogo {
NSString *path = [[NSBundle mainBundle] pathForResource:@"logo" 
                                                 ofType:@"m4v" 
                                            inDirectory:@"../Documents"];

self.myplayerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:path]];
self.myplayer = [AVPlayer playerWithPlayerItem:self.myplayerItem];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[self.myplayer currentItem]];
[videoView setPlayer:self.myplayer];
[videoView setAlpha:0.5f];
[self.myplayer play]; }

- (void)playerItemDidReachEnd:(NSNotification *)notification {
[videoView setAlpha:0.0f];
[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:AVPlayerItemDidPlayToEndTimeNotification 
                                              object:[self.myplayer currentItem]];
[self playLogo];    }
于 2011-02-08T14:41:48.373 回答
0

in Controller.h add

AVPlayer *myplayer;

in Controller.m add

#import <CoreMedia/CoreMedia.h>

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[myplayer currentItem]];
[myplayer play];

and add function from selector

- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *playerItem = [notification object];
[playerItem seekToTime:kCMTimeZero];
[myplayer play];

}

This is help me for looping video, but you need cache it some how (for smooth loop)

于 2011-02-07T05:36:31.157 回答