1 Replies - 922 Views - Last Post: 31 May 2016 - 09:51 PM

#1 moonman239  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-March 15

AVQueuePlayer not replaying and workaround not working

Posted 30 May 2016 - 11:53 PM

Problem:
My AVQueuePlayer doesn't seem to be playing the last item in the queue after a rewind to the beginning.
My code
-(void)doReplay:(BOOL)insertLastItem

{    
    AVPlayerItem *lastItem = [self.queuePlayer currentItem];
    [self.queuePlayer pause];
    [lastItem seekToTime:kCMTimeZero toleranceBefore:CMTimeMake(0,10) toleranceAfter:CMTimeMake(1,10) completionHandler:^(BOOL finished){
        if (finished)
        {
        NSLog(@"Time: %f",CMTimeGetSeconds(lastItem.currentTime));
        if (lastItem.status == AVPlayerItemStatusReadyToPlay) {
            [self.queuePlayer play];

            [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
            BOOL isItPlaying = [self.queuePlayer rate] > 0;
            NSLog(@"Playing? %@", isItPlaying ? @"Yes" : @"No");
        }
        else
        {
            NSLog(@"Not ready to play");
        }
        }
    }];
    
}


My attempt at a solution:
In the following code, lastAsset appears to be nil, although it is (supposedly) set prior to the method call:
-(void)doReplay:(BOOL)insertLastItem
{
/*
    if (insertLastItem) {
        [self.queuePlayer insertItem:lastItem afterItem:nil];
    }*/
    AVPlayerItem *copyItem = [AVPlayerItem playerItemWithAsset:lastAsset];
    [self.queuePlayer insertItem:copyItem afterItem:nil];
        [self.queuePlayer pause];
    [self.queuePlayer seekToTime:kCMTimeZero completionHandler:^(BOOL finished){
        [self.queuePlayer play];
    }];
}


I've modified this code, though, and I'll test it to see if the problem still arises.

Is This A Good Question/Topic? 0
  • +

Replies To: AVQueuePlayer not replaying and workaround not working

#2 moonman239  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-March 15

Re: AVQueuePlayer not replaying and workaround not working

Posted 31 May 2016 - 09:51 PM

Well, this code works (on a simulator, at least):
-(void)doReplay:(BOOL)insertLastItem
{
/*
    if (insertLastItem) {
        [self.queuePlayer insertItem:lastItem afterItem:nil];
    }*/
    AVPlayerItem *lastItem = [self.queuePlayer.items lastObject];
    AVAsset *lastAsset = [lastItem asset];
    AVPlayerItem *copyItem = [AVPlayerItem playerItemWithAsset:lastAsset];
    [self.queuePlayer insertItem:copyItem afterItem:nil];
        [self.queuePlayer pause];
    [self.queuePlayer seekToTime:kCMTimeZero completionHandler:^(BOOL finished){
        [self.queuePlayer play];
    }];
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1