AVQueuePlayer not replaying last item

Discussion in 'iOS Programming' started by moonman239, May 16, 2016.

  1. moonman239, May 16, 2016
    Last edited: May 27, 2016

    moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    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.
     
  2. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #2
    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];
    }
    
     
  3. moonman239, May 18, 2016
    Last edited: Jun 1, 2016

    moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    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.
     
  4. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #4
    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.
     

Share This Page