最后重播AVQueuePlayer中的项目

Ede*_*iss 8 iphone avfoundation ios avplayer avqueueplayer

我需要在我的内容中创建类似无限循环的东西AVQueuePlayer.特别是,我要重播整个NSArrayAVPlayerItem一度的最后一个组件播放完毕.

我必须承认,我实际上并不知道如何实现这一点,并希望你能给我一些线索.

mpe*_*urn 2

这几乎是从头开始的。其组成部分是:

  1. 创建一个队列,该队列是 AVPlayerItems 的 NSArray。
  2. 当每个项目添加到队列中时,设置一个 NSNotificationCenter 观察者以在视频到达末尾时唤醒。
  3. 在观察者的选择器中,告诉 AVPlayerItem 您要循环返回开头,然后告诉播放器播放。

(注意:AVPlayerDemoPlaybackView 来自 Apple“AVPlayerDemo”示例。只是带有 setter 的 UIView 的子类)

BOOL videoShouldLoop = YES;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableArray *videoQueue = [[NSMutableArray alloc] init];
AVQueuePlayer *mPlayer;
AVPlayerDemoPlaybackView *mPlaybackView;

// You'll need to get an array of the files you want to queue as NSARrray *fileList:
for (NSString *videoPath in fileList) {
    // Add all files to the queue as AVPlayerItems
    if ([fileManager fileExistsAtPath: videoPath]) {
        NSURL *videoURL = [NSURL fileURLWithPath: videoPath];
        AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL: videoURL];
        // Setup the observer
        [[NSNotificationCenter defaultCenter] addObserver: self
                                                 selector: @selector(playerItemDidReachEnd:)
                                                     name: AVPlayerItemDidPlayToEndTimeNotification
                                                   object: playerItem];
        // Add the playerItem to the queue
        [videoQueue addObject: playerItem];
    }
}
// Add the queue array to the AVQueuePlayer
mPlayer = [AVQueuePlayer queuePlayerWithItems: videoQueue];
// Add the player to the view
[mPlaybackView setPlayer: mPlayer];
// If you should only have one video, this allows it to stop at the end instead of blanking the display
if ([[mPlayer items] count] == 1) {
    mPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
}
// Start playing
[mPlayer play];


- (void) playerItemDidReachEnd: (NSNotification *)notification
{
    // Loop the video
    if (videoShouldLoop) {
        // Get the current item
        AVPlayerItem *playerItem = [mPlayer currentItem];
        // Set it back to the beginning
        [playerItem seekToTime: kCMTimeZero];
        // Tell the player to do nothing when it reaches the end of the video
        //  -- It will come back to this method when it's done
        mPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
        // Play it again, Sam
        [mPlayer play];
    } else {
        mPlayer.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;
    }
}
Run Code Online (Sandbox Code Playgroud)

就是这样!让我知道一些需要进一步解释的事情。