Ede*_*iss 8 iphone avfoundation ios avplayer avqueueplayer
我需要在我的内容中创建类似无限循环的东西AVQueuePlayer.特别是,我要重播整个NSArray的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)
就是这样!让我知道一些需要进一步解释的事情。
| 归档时间: |
|
| 查看次数: |
6008 次 |
| 最近记录: |