2012-06-22 8 views
7

AVQueuePlayerに無限ループのようなものを作成する必要があります。特に、最後のコンポーネントの再生が完了したら、AVPlayerItemNSArrayを再生したいと思います。最後に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が 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

私はplayer.And 3videoすべての完了後に最後のビデオは無限ループで再生されます追加された場合はどうすればよいですか? –

+0

ここでのロジックは、OPの希望の動作を取得しません。最後のアイテムは、プレイヤーアイテムの配列全体ではなくループします。 – Joey

+0

それは私のために働いてくれてありがとう.. –

0

私はビデオキュー内のすべてのビデオを1つだけでなくループして解決する方法を見つけました。ループに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を挿入して再生待ちにします。各項目ごとにゼロを求めてください。 advanceToNextItemの後にAVQueuePlayerは、currentItemをキューから削除します。

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