2012-06-22 31 views
7

Tôi cần tạo một thứ gì đó giống như vòng lặp vô hạn trong số AVQueuePlayer của tôi. Đặc biệt, tôi muốn phát lại toàn bộ số NSArray của AVPlayerItem giây khi thành phần cuối cùng kết thúc phát.Phát lại các mục trong AVQueuePlayer sau

Tôi phải thừa nhận rằng tôi thực sự không có bất kỳ ý tưởng làm thế nào để đạt được điều này và hy vọng bạn có thể cho tôi một số manh mối.

+0

Bạn có bị kẹt vào thời điểm này hoặc bạn chỉ cần tạo từ điểm xuất phát? – Dhruv

+0

Tôi thực sự bây giờ làm thế nào để tạo ra nó và chơi tất cả các AVQueuePlayers, tôi bây giờ đang tìm kiếm khởi động lại máy nghe nhạc khi QVPlayerItem cuối cùng được thực hiện. – Edelweiss

+0

'- (void) playVideoAtIndex: (NSInteger) chỉ mục { [self performSelector: @selector (setObservationInfo)]; currentIndex = index; AVPlayerItem * videoItem = [AVPlayerItem playerItemWithURL: [NSURL fileURLWithPath: [đối tượng arrVideoListAtIndex: index]]]; } ' nơi bạn cần kiểm tra, ' if (currentIndex <[arrVideoList count] -1) { currentIndex ++; } else { currentIndex = 0; } [tự phátVideoAtIndex: currentIndex]; ' – Dhruv

Trả lời

1

Điều này là khá nhiều từ đầu. Các thành phần là:

  1. Tạo hàng đợi là NSArray của AVPlayerItems.
  2. Khi mỗi mục được thêm vào hàng đợi, hãy thiết lập một người quan sát NSNotificationCenter để thức dậy khi video kết thúc.
  3. Trong bộ chọn của người quan sát, hãy nói cho AVPlayerItem biết bạn muốn lặp lại để bắt đầu lại, sau đó yêu cầu người chơi phát.

(Chú ý: Các AVPlayerDemoPlaybackView xuất phát từ Apple "AVPlayerDemo" mẫu Đơn giản chỉ cần một lớp con của UIView với một setter.)

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; 
    } 
} 

Vậy là xong! Hãy cho tôi biết điều gì đó cần giải thích thêm.

+0

Tôi nên làm gì trong trường hợp tôi thêm 3video vào trình phát. Sau khi hoàn thành tất cả video 3video, video cuối cùng sẽ phát trong vòng lặp vô hạn –

+0

Logic ở đây không có được hành vi mong muốn của OP. Nó lặp lại mục cuối cùng không phải toàn bộ mảng các mục của người chơi. – Joey

+0

Cảm ơn bạn đã làm việc cho tôi .. –

0

Tôi đã tìm ra giải pháp để lặp qua tất cả các video trong hàng đợi video của mình, không chỉ một video. Trước tiên tôi khởi của tôi 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]; 
} 

Khi thông báo được đăng

- (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

Cách tốt nhất để vòng một chuỗi các video trong AVQueuePlayer.

quan sát từng playeritem trong AVQueuePlayer.

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

trên mỗi lần tiếp theoVideo chèn currentItem lần nữa để xếp hàng để phát lại. đảm bảo tìm kiếm bằng không cho mỗi mục. after advanceToNextItem AVQueuePlayer sẽ loại bỏ currentItem khỏi hàng đợi.

-(void) nextVideo:(NSNotification*)notif { 
    AVPlayerItem *currItem = notif.userInfo[@"object"]; 
    [currItem seekToTime:kCMTimeZero]; 
    [queuePlayer advanceToNextItem]; 
    [queuePlayer insertItem:currItem afterItem:nil]; 
} 
Các vấn đề liên quan