2012-06-22 14 views
7

Ich muss so etwas wie eine Endlosschleife in meinem AVQueuePlayer erstellen. Vor allem möchte ich die ganze NSArray von AVPlayerItem s wiedergeben, sobald die letzte Komponente beendet ist zu spielen.Replay Elemente in AVQueuePlayer nach dem letzten

Ich muss zugeben, dass ich eigentlich keine Ahnung habe, wie ich das erreichen kann und hoffe, dass Sie mir ein paar Hinweise geben können.

+0

Sind Sie an diesem Punkt fest oder müssen Sie es nur vom Ausgangspunkt erstellen? – Dhruv

+0

Ich habe jetzt eigentlich wie man es erstellt und alle AVQueuePlayer abspielt, ich suche jetzt nach dem Neustart des Players wenn das letzte QVPlayerItem fertig ist. – Edelweiss

+0

'- (void) playVideoAtIndex: (NSInteger) index { [self performSelector: @selector (setObservationInfo)]; currentIndex = Index; AVPlayerItem * videoItem = [AVPlayerItem playerItemWithURL: [NSURL-DateiURLWithPath: [arrVideoList objectAtIndex: index]]]; } ' wo Sie überprüfen müssen, ' if (Momentanindex <[arrVideoList count] -1) { Momentan ++; } sonst { currentIndex = 0; } [self playVideoAtIndex: currentIndex]; ' – Dhruv

Antwort

1

Das ist es von Grund auf neu. Die Komponenten sind:

  1. Erstellen Sie eine Warteschlange, die ein NSArray von AVPlayerItems ist.
  2. Wenn jedes Element zur Warteschlange hinzugefügt wird, richten Sie einen NSNotificationCenter-Beobachter ein, der aktiviert wird, wenn das Video das Ende erreicht.
  3. Sagen Sie im Selektor des Beobachters dem AVPlayerItem, dass Sie eine Schleife erstellen möchten, um den Anfang zurückzugehen, und sagen Sie dem Spieler, dass er spielen soll.

(HINWEIS: Die AVPlayerDemoPlaybackView kommt aus dem Apple "AVPlayerDemo" Probe einfach eine Unterklasse von UIView mit einem 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; 
    } 
} 

Das ist es! Lassen Sie mich wissen, dass etwas eine weitere Erklärung benötigt.

+0

Was soll ich tun, falls ich 3video im Player hinzufüge.Und nach Abschluss aller 3video wird das letzte Video in Endlosschleife abgespielt –

+0

Die Logik hier erhält nicht das gewünschte Verhalten des OP. Es wird das letzte Element und nicht das gesamte Array von Player-Elementen durchlaufen. – Joey

+0

Danke, es hat für mich funktioniert .. –

0

Ich fand eine Lösung, um alle Videos in meiner Videowarteschlange zu überspielen, nicht nur eine. Zuerst initialisiert ich meinen 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]; 
} 

Wenn die Benachrichtigung

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

besten Weg, um Schleife eine Folge von Videos in AVQueuePlayer gebucht wird.

beobachten Sie für jeden playeritem in AVQueuePlayer.

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

In jedes nextVideo fügen Sie das currentItem erneut ein, um es für die Wiedergabe in die Warteschlange zu stellen. Stellen Sie sicher, für jeden Artikel auf Null zu suchen. Nach advanceToNextItem entfernt der AVQueuePlayer das currentItem aus der Warteschlange.

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