iOS AVQueuePlayer not replaying last item

moonman239

macrumors 68000
Original poster
Mar 27, 2009
1,530
22
I have a method that I use to automatically replay the last item in AVQueuePlayer's queue. The problem is, it doesn't actually do a replay when it's called.

Here's the method:
Code:
-(void)doReplay:(BOOL)insertLastItem

{

    if (insertLastItem) {

        [self.queuePlayerinsertItem:lastItemafterItem:nil];

    }

        [self.queuePlayer seekToTime:kCMTimeZero];

        [self.queuePlayer play];

}
I've verified that the above method is called after the item in question has finished playing. Will someone familiar with AVQueuePlayer please tell me why my method isn't working?
Oh, perhaps I need to "pause" or "stop" first?

Edit: I set a breakpoint in the method and checked
Code:
[self.queuePlayer items]
and the debugger said the item was still in the queue.

Edit: All the media I'm using is local.
 
Last edited:

moonman239

macrumors 68000
Original poster
Mar 27, 2009
1,530
22
So, I still don't have a solution to my problem. However, I do have a workaround:
Code:
-(void)doReplay:(BOOL)insertLastItem
{
[self.queuePlayer pause];
[self.queuePlayer insertItem:[AVPlayerItem playerItemWithAsset:[lastItem asset]] afterItem:nil];
[self.queuePlayer play];
}
 

moonman239

macrumors 68000
Original poster
Mar 27, 2009
1,530
22
Here's the code for a test program I made in an attempt to solve the problem:
Code:
- (void)viewDidLoad {

    [superviewDidLoad];

    self.queuePlayer = [[AVQueuePlayer alloc] init];

 

    AVPlayerItem *playerItem1 = [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"placeholder" withExtension:@"m4a"]];

    [self.queuePlayer insertItem:playerItem1 afterItem:nil];

    AVPlayerItem *item2 = [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@"second-sound" withExtension:@"m4a"]];

    [self.queuePlayer insertItem:item2 afterItem:nil];

    [[NSNotificationCenterdefaultCenter] addObserver:self selector:@selector(shouldReplay) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

    [self.queuePlayer play];

}



-(void)doReplay:(BOOL)insertLastItem



{

    /*

    if (insertLastItem) {

        [self.queuePlayer insertItem:lastItem afterItem:nil];

     

    }*/

    AVPlayerItem *lastItem = [self.queuePlayer currentItem];

    [lastItem seekToTime:kCMTimeZero];

}





- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

-(void)shouldReplay

    {

        if ([[self.queuePlayer items] count] == 1)

        {

           

            [self doReplay:false];

        }

       

    }
To the best of my knowledge, both the player item and the player were ready to play just prior to the "play" command in the method.
 
Last edited:

moonman239

macrumors 68000
Original poster
Mar 27, 2009
1,530
22
OK, my workaround isn't exactly working. doReplay seems to be inserting a nil item. My debugger suggests this is because [lastItem asset] is nil.

BTW, apologies for the third thread over 2 years on the subject of replaying.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.