Error: "An AVPlayerItem can occupy only one position in a player's queue at a time."

Discussion in 'iOS Programming' started by moonman239, Jul 30, 2014.

  1. moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    I'm working on a new app. One of this app's view controllers has labels that can be dragged and dropped into any of the controller's collection view's cells. When it is, if the label is supposed to be in the cell it's in, then a sound is to be played. I'm not deliberately inserting the sound's corresponding player item twice, but I got the error message described in the title.

    Below, I have posted some code to help you figure out why I got the error:
    Code:
    -(void)label:(UILabel *)label wasDraggedToPoint:(CGPoint)point
    {
        TextCell *theCell;
        CGRect collectionViewFrame = [[self collectionView] frame];
        if (CGRectContainsPoint(collectionViewFrame, point)) {
            // The user dragged a label, and dropped it inside the collection view that displays underscores.
            NSInteger index = 0;
            // Convert the coordinates of point to coordinates that are relative to the collection view.
            point = [[self view] convertPoint:point toView:[self collectionView]];
            for (TextCell *cell in [[self collectionView] visibleCells])
            {
                CGRect frame = [cell frame];
                if (CGRectContainsPoint(frame, point)) {
                    // The user dropped the label inside the cell that is represented by the variable "cell."
                    theCell = cell;
                    index = [[[self collectionView] indexPathForCell:theCell] row];
                }
            }
            if (theCell != nil) {
                NSInteger indexOfLabelText = [blahComponents indexOfObject:[label text]];
                if (indexOfLabelText == index) {
                    // The user dragged the label to the correct cell.
                    [[theCell textLabel] setText:[label text]];
                    [label setHidden:true];
                    NSURL *rswmURL = [[NSBundle mainBundle] URLForResource:@"rswm" withExtension:@"m4a" subdirectory:@"sounds"];
                    AVPlayerItem *rswmItem = [[AVPlayerItem alloc] initWithURL:rswmURL];
                    [[self queuePlayer] insertItem:rswmItem afterItem:nil];
                    NSURL *blahSoundFileURL = [[NSBundle mainBundle] URLForResource:blah withExtension:@"m4a" subdirectory:@"sounds"];
                    AVPlayerItem *blahSoundPlayerItem = [[AVPlayerItem alloc] initWithURL:blahSoundFileURL];
                    [[self queuePlayer] insertItem:rswmItem afterItem:nil];  // This line triggers an exception breakpoint.
                    [[self queuePlayer] insertItem:blahSoundPlayerItem afterItem:nil];
                    [[self queuePlayer] play];
                    
                }
                else
                {
                    // The user did not drag the label to the correct cell.
                    [label setCenter:[(DraggableLabel *)label originalLocation]];
                }
            }
        }
        
    }
    
     
  2. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #2
    Did you actually read the code or just copy-pasted? ;)

    Code:
    AVPlayerItem *rswmItem = [[AVPlayerItem alloc] initWithURL:rswmURL];
    [B][[self queuePlayer] insertItem:rswmItem afterItem:nil];[/B]
    NSURL *blahSoundFileURL = [[NSBundle mainBundle] URLForResource:blah withExtension:@"m4a" subdirectory:@"sounds"];
    AVPlayerItem *blahSoundPlayerItem = [[AVPlayerItem alloc] initWithURL:blahSoundFileURL];
    [B][[self queuePlayer] insertItem:rswmItem afterItem:nil];  // This line triggers an exception breakpoint.[/B]
    [B][[self queuePlayer] insertItem:blahSoundPlayerItem afterItem:nil];[/B]
    
     
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    I noticed that last night. :rolleyes: Thanks, anyways!
     
  4. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #4
    Cool, don't forget to mark as resolved and try not make copy-paste code a habit, it is bad and you won't ever learn anything from it. I bet you still don't know exactly what your code does and why it is wrong ;)
     

Share This Page