AVQueuePlayer playback without gap and freeze

后端 未结 3 1757
无人及你
无人及你 2020-12-05 12:19

I use AVQueuePlayer to play a sequence of movies which are loaded from URLs.
I tried to initialize player instance with array of all AVPlayerItems

相关标签:
3条回答
  • 2020-12-05 12:24

    The solution is found.

    When adding new AVPlayerItem in queue of AVQueuePlayer player will synchronously wait till initial part of player item will be buffered.

    So in this case player item should be buffered asynchronously and after that it can be added in the queue.
    It can be done using [AVURLAsset loadValuesAsynchronouslyForKeys: completionHandler:]

    For example:

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
    NSArray *keys = [NSArray arrayWithObject:@"playable"];
    
    [asset loadValuesAsynchronouslyForKeys:keys completionHandler:^()
    {
        dispatch_async(dispatch_get_main_queue(), ^
        {
            AVPlayerItem *playerItem = [[[AVPlayerItem alloc] initWithAsset:asset] autorelease];         
            [player insertItem:playerItem afterItem:nil]; 
        });
    
    }];
    

    Using this solution queue of AVQueuePlayer can be populated with items without any gaps and freezes.

    0 讨论(0)
  • 2020-12-05 12:25

    in Swift 2, working here:

    func load() {
         let player = AVQueuePlayer()
         for url in urls {
            makeItem(url)
        }
    }
    
    func makeItem(url: String) {
        let avAsset = AVURLAsset(URL: NSURL(string: url)!)
    
        avAsset.loadValuesAsynchronouslyForKeys(["playable", "tracks", "duration"], completionHandler: {
            dispatch_async(dispatch_get_main_queue(), {
                self.enqueue(avAsset: avAsset)
            })
        })
    }
    
    func enqueue(avAsset: AVURLAsset) {
        let item = AVPlayerItem(asset: avAsset)
        self.player.insertItem(item, afterItem: nil)
    }
    
    0 讨论(0)
  • 2020-12-05 12:33

    Here is solution.

    - (void)_makePlayer{
         _player = [[AVQueuePlayer alloc] initWithPlayerItem:[AVPlayerItem playerItemWithAsset:[SSMoviePreviewItemMaker generateAVMovieItem]]];
    }
    
    + (AVAsset *)generateAVMovieItem{
        NSArray * array = [SSMovieFileManager getAllMovieResourceURL];
        AVMutableComposition *composition = [[AVMutableComposition alloc] init];
        for (int i = 0; i < array.count; i++) {
            AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:array[i] options:nil];
            [composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                                 ofAsset:asset
                                  atTime:composition.duration error:nil];
    
        }
       return composition;
    }
    
    0 讨论(0)
提交回复
热议问题