0

私のアプリでは、最初のビデオを一度に再生し、次に 2 番目のビデオを再生する必要があります。これは繰り返されます。というわけで、最初はMPMoviePlayerControllerを使っていたのですが、すでに2つの動画の間に黒い画面ができていました。だから、私の場合は AVFoundation を使うべきだということをどこかで読みました。これが、NSURL で作成した 2 つの AVPLayerItem で AVQueuePLayer を使用する理由です。しかし、問題は、最初のビデオが終了した後、長い一時停止があることです。2番目のビデオが始まる前の0.5秒近く。どのように削除しますか?使い方は?だから、私は本当に助けていただきありがとうございます!できるだけ早くそれが必要です。ありがとう!

ここで私が使用するすべての方法:

- (void) configureAVPlayer {
   self.queuePlayer.actionAtItemEnd = AVPlayerActionAtItemEndPause; 

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

   self.isFirstVideoFinished = NO;

   [self playVideos];

}

- (void) playVideos {
   [self setPlayerItemsForQueue];
   [self.videoHolder setPlayer:self.queuePlayer];

}

- (void) setPlayerItemsForQueue {
NSURL *fileURL = [[NSBundle mainBundle]
                  URLForResource:[self getStringForMode:self.currentMode] withExtension:@"mp4"];

NSMutableString *secondFile = [self getStringForMode:self.currentMode];
[secondFile appendString:@"Second"];
NSURL *secondFileUrl = [[NSBundle mainBundle] URLForResource:secondFile withExtension:@"mp4"];

AVAsset *firstAsset = [AVAsset assetWithURL:fileURL];

AVPlayerItem *firstVideoItem = [AVPlayerItem playerItemWithAsset:firstAsset];//[AVPlayerItem playerItemWithURL:fileURL];

AVAsset *secondAsset = [AVAsset assetWithURL:secondFileUrl];
AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithAsset:secondAsset];//[AVPlayerItem playerItemWithURL:secondFileUrl];

self.queuePlayer = [AVQueuePlayer queuePlayerWithItems: [NSArray arrayWithObjects:firstVideoItem, secondVideoItem,nil]];

for (AVPlayerItem *playerItem in self.queuePlayer.items) {
    [playerItem addObserver: self forKeyPath: @"status" options:0 context:NULL];
}

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
                    change:(NSDictionary *)change context:(void *)context {

dispatch_async(dispatch_get_main_queue(), ^{
    if ([(AVPlayerItem *)object status] == AVPlayerItemStatusReadyToPlay) {
        [self.queuePlayer play];
    }
});

}

- (void) playerItemDidReachEnd:(NSNotification*) notification {

NSMutableString *secondFile = [self getStringForMode:self.currentMode];
[secondFile appendString:@"Second"];
NSURL *secondFileUrl = [[NSBundle mainBundle] URLForResource:secondFile withExtension:@"mp4"];
AVPlayerItem *playerItem = [[ AVPlayerItem alloc ] initWithURL:secondFileUrl];

if ([self.queuePlayer canInsertItem:playerItem afterItem:[notification object]]) {
    [self.queuePlayer insertItem: playerItem afterItem: nil ];
}

}

4

1 に答える 1

0

はい、複数の AVPlayer オブジェクトが最適なオプションです。

この問題は、firstVideoItem の現在の再生時間を監視するメイン タイマー関数で解決し、終了から 0.01 秒になったときに、アルファ チャネルでプレーヤーを反転させて、secondVideoItem を表示してから、secondVideoItem を再生しました。

また、再生コマンドを発行する前に [secondVideoItem seekToTime:CMTimeMakeWithSeconds(1,1)] を使用して、2 番目のビデオが初期化され、再生の準備ができていることを確認しました。これは大いに役立ちました。

于 2012-03-27T10:03:49.640 に答える