From 17d68ba5dca1e4f80d0491998919444c841df808 Mon Sep 17 00:00:00 2001 From: Karol Polaszek Date: Tue, 21 Jun 2016 11:03:37 +0200 Subject: [PATCH] Prevent freeze app on streaming --- HysteriaPlayer/HysteriaPlayer.m | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/HysteriaPlayer/HysteriaPlayer.m b/HysteriaPlayer/HysteriaPlayer.m index 817d8ae..5afdc14 100755 --- a/HysteriaPlayer/HysteriaPlayer.m +++ b/HysteriaPlayer/HysteriaPlayer.m @@ -305,19 +305,28 @@ - (void)getSourceURLAtIndex:(NSInteger)index preBuffer:(BOOL)preBuffer - (void)setupPlayerItemWithUrl:(NSURL *)url index:(NSInteger)index { - AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url]; - if (!item) - return; + AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil]; + NSArray *keys = @[@"playable"]; - dispatch_async(dispatch_get_main_queue(), ^{ - [self setHysteriaIndex:item key:[NSNumber numberWithInteger:index]]; - if (self.isMemoryCached) { - NSMutableArray *playerItems = [NSMutableArray arrayWithArray:self.playerItems]; - [playerItems addObject:item]; - self.playerItems = playerItems; - } - [self insertPlayerItem:item]; - }); + AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset]; + + [asset loadValuesAsynchronouslyForKeys:keys completionHandler:^() { + + dispatch_async(dispatch_get_main_queue(), ^{ + + [self setHysteriaIndex:item key:[NSNumber numberWithInteger:index]]; + + if (self.isMemoryCached) { + NSMutableArray *playerItems = [NSMutableArray arrayWithArray:self.playerItems]; + [playerItems addObject:item]; + self.playerItems = playerItems; + } + + if ([self getLastItemIndex] == index) { + [self insertPlayerItem:item]; + } + }); + }]; }