Video multipli con AVQueuePlayer

La classe MPMoviePlayerController ha il grosso difetto di non gestire adeguatamente più video contemporanei, oltre a rendere difficile creare una sequenza di video uno dopo l’altro. La classe AVPlayer (o AVQueuePlayer che è una sottoclasse di AVPlayer), appartenente al framework più a basso livello AVFoundation, è invece estremamente più versatile, nonostante mantenga egualmente una semplicità nell’implementazione.

Frameworks Block Diagram

Lasciatemi mostrare come sia semplice riprodurre due video in sequenza:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Inserite il framework AVFoundation
#import <AVFoundation/AVFoundation.h>
...
// Definizione
AVQueuePlayer *avPlayer;
AVPlayerItem *avPlayerItemOne;
AVPlayerItem *avPlayerItemTwo;
...

// Inizializza sequenze video
- (void)initAndPlayMovie
{
  avPlayerItemOne = [[AVPlayerItem alloc] initWithURL:
                         [NSURL fileURLWithPath:
                          [[NSBundle mainBundle] pathForResource:@"movie1" ofType:@"mp4"]]];

  avPlayerItemTwo = [[AVPlayerItem alloc] initWithURL:
                         [NSURL fileURLWithPath:
                          [[NSBundle mainBundle] pathForResource:@"movie2" ofType:@"mov"]]];
   
  NSArray *playerItems = [[NSArray alloc] initWithObjects:avPlayerItemOne, avPlayerItemTwo, nil];
  avPlayer = [[AVQueuePlayer alloc] initWithItems:playerItems];
  [playerItems release];
       
  AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
  [avPlayerLayer setFrame:CGRectMake(0, 0, 1024., 768.)];
  [self.view.layer addSublayer:avPlayerLayer];
  [avPlayer play];
   
  avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
   
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[avPlayer currentItem]];
}

...

// Notifica fine filmato... passo al possimo
- (void)playerItemDidReachEnd:(NSNotification *)notification
{    
    [avPlayer advanceToNextItem];
}

Non ci sono commenti per questo Post

Lascia un commento

TAG XHTML PERMESSI: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERIMENTO CODICE:
<pre></pre> // blocco generico
					<code></code> // blocco generico
					[cc_actionscript][/cc_actionscript] // Actionscript
					[cc_actionscript3][/cc_actionscript3] // Actionscript 3
					[cc_css][/cc_css] // CSS Style Sheet
					[cc_html][/cc_html] // HTML
					[cc_js][/cc_js] // Javascript
					[cc_objc][/cc_objc] // Objective-C
					[cc_php][/cc_objc] // PHP
					[cc_sql][/cc_sql] // SQL


Stop SOPA