2012-06-22 5 views
7

AVQueuePlayer에 무한 루프와 같은 것을 만들어야합니다. 특히, 마지막 구성 요소 재생이 완료되면 AVPlayerItem 전체 NSArray을 재생하려고합니다.마지막으로 AVQueuePlayer의 항목을 재생하십시오.

나는 이것을 실제로 어떻게 달성 할 수 있는지 전혀 알지 못하지만, 당신이 나에게 단서를 줄 수 있기를 바랍니다.

+0

이 시점에서 멈췄습니까? 아니면 시작점에서 생성해야합니까? – Dhruv

+0

나는 실제로 그것을 만들고 모든 AVQueuePlayers를 재생하는 법을 배우기 위해 마지막 QVPlayerItem이 완료되었을 때 플레이어를 다시 시작하려고합니다. – Edelweiss

+0

'- (void) playVideoAtIndex : (NSInteger) index { [self performSelector : @selector (setObservationInfo)]; currentIndex = index; AVPlayerItem * videoItem = [AVPlayerItem playerItemWithURL : [NSURL fileURLWithPath : [arrVideoList objectAtIndex : index]]]; }'확인할 필요 , 경우 '(currentIndex는 <[arrVideoList 카운트] -1) { currentIndex ++; } else { currentIndex = 0; } [self playVideoAtIndex : currentIndex]; ' – Dhruv

답변

1

처음부터 꽤 많이 있습니다. 구성 요소는 다음과 같습니다.

  1. AVPlayerItems의 NSArray 인 큐를 만듭니다.
  2. 각 항목이 대기열에 추가 될 때 NSNotificationCenter 관찰자가 비디오가 끝날 때 깨어나도록 설정하십시오.
  3. 관찰자의 선택기에서 AVPlayerItem에 루프를 반복하여 처음으로 돌아가라고 말한 다음 재생하도록 플레이어에게 알립니다.

(참고 : AVPlayerDemoPlaybackView 애플 "AVPlayerDemo"에서 유래 샘플 세터와의 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; 
    } 
} 

입니다! 더 많은 설명이 필요하다는 것을 알려주지.

+0

플레이어에서 3video를 추가 한 경우 어떻게해야합니까? 그리고 모든 3video 완료 후 마지막 비디오가 무한 루프로 재생됩니다. –

+0

여기 논리가 OP의 원하는 동작을 얻지 못합니다. 마지막 항목이 플레이어 항목의 전체 배열이 아닌 루프됩니다. – Joey

+0

저에게 감사드립니다. –

0

비디오 큐의 모든 비디오를 하나의 루프가 아닌 하나의 비디오 큐에 넣을 수있는 솔루션을 찾았습니다. 루프 AVQueuePlayer에있는 동영상의 순서를

- (void)viewDidLoad 
{ 
    NSMutableArray *vidItems = [[NSMutableArray alloc] init]; 
    for (int i = 0; i < 5; i++) 
    { 
     // create file name and make path 
     NSString *fileName = [NSString stringWithFormat:@"intro%i", i]; 
     NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mov"]; 
     NSURL *movieUrl = [NSURL fileURLWithPath:path]; 
     // load url as player item 
     AVPlayerItem *item = [AVPlayerItem playerItemWithURL:movieUrl]; 
     // observe when this item ends 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(playerItemDidReachEnd:) 
                name:AVPlayerItemDidPlayToEndTimeNotification 
                object:item]; 
     // add to array 
     [vidItems addObject:item]; 


    } 
    // initialize avqueueplayer 
    _moviePlayer = [AVQueuePlayer queuePlayerWithItems:vidItems]; 
    _moviePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

    // create layer for viewing 
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_moviePlayer]; 

    layer.frame = self.view.bounds; 
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    // add layer to uiview container 
    [_movieViewContainer.layer addSublayer:layer]; 
} 

통지가 게시

- (void)playerItemDidReachEnd:(NSNotification *)notification { 
    AVPlayerItem *p = [notification object]; 

    // keep playing the queue 
    [_moviePlayer advanceToNextItem]; 
    // if this is the last item in the queue, add the videos back in 
    if (_moviePlayer.items.count == 1) 
    { 
     // it'd be more efficient to make this a method being we're using it a second time 
     for (int i = 0; i < 5; i++) 
     { 
      NSString *fileName = [NSString stringWithFormat:@"intro%i", i]; 
      NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mov"]; 
      NSURL *movieUrl = [NSURL fileURLWithPath:path]; 

      AVPlayerItem *item = [AVPlayerItem playerItemWithURL:movieUrl]; 

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

      // the difference from last time, we're adding the new item after the last item in our player to maintain the order 
      [_moviePlayer insertItem:item afterItem:[[_moviePlayer items] lastObject]]; 
     } 
    } 
} 
0

가장 좋은 방법 : 우선 내 AVQueuePlayer를 초기화.

AVQueuePlayer의 각 플레이어 항목을 관찰합니다.

queuePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 
for(AVPlayerItem *item in items) { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
      selector:@selector(nextVideo:) 
      name:AVPlayerItemDidPlayToEndTimeNotification 
      object:item ]; 
} 

각 nextVideo에 currentItem을 다시 삽입하여 재생 대기열에 넣습니다. 각 항목에 대해 0을 찾아야합니다. advanceToNextItem 후에 AVQueuePlayer는 queue에서 currentItem을 제거합니다.

-(void) nextVideo:(NSNotification*)notif { 
    AVPlayerItem *currItem = notif.userInfo[@"object"]; 
    [currItem seekToTime:kCMTimeZero]; 
    [queuePlayer advanceToNextItem]; 
    [queuePlayer insertItem:currItem afterItem:nil]; 
} 
관련 문제