Transferring Music in iOS via Bluetooth

70 Views Asked by At

I have set it up to where music can be sent back and forth between iOS devices, but whenever the music is sent, the receiving phone gets this error:

*** Terminating app due to uncaught exception 'MPMediaItemCollectionInitException', reason: 'items array must not be empty' *** First throw call stack:

This is the method where I get the error:

-(void)didReceiveDataWithNotification: (NSNotification *)notification{
    MCPeerID *peerID = [[notification userInfo] objectForKey:@"peerID"];
    NSString *peerDisplayName = peerID.displayName;
    NSData *receivedData = [[notification userInfo] objectForKey:@"data"];
    //NSLog([[notification userInfo] objectForKey:@"data"]);
    NSDictionary *dict = (NSDictionary *)[NSKeyedUnarchiver unarchiveObjectWithData:receivedData];
    NSString *datatype = [dict objectForKey:@"datatype"];
    NSLog(@"hi");
    if([datatype isEqualToString:@"musiclist"]){
        count++;
        MPMediaItemCollection *collectionFromPeer = [[MPMediaItemCollection alloc] initWithItems:[(MPMediaItemCollection*)[dict objectForKey:@"data"] items]];
        [_listOfMusic addObjectsFromArray:[collectionFromPeer items]];
        if(count == [[_appDelegate.mcManager.session connectedPeers] count] + 1){
            for(int i=0;i<[_listOfMusic count];i++){
                int r1 = arc4random() % [_listOfMusic count];
                int r2 = arc4random() % [_listOfMusic count];
                [_listOfMusic exchangeObjectAtIndex:r1 withObjectAtIndex:r2];
            }
            MPMediaItemCollection *coll = [[MPMediaItemCollection alloc]initWithItems:_listOfMusic];
            [_musicPlayer setQueueWithItemCollection:coll];
            NSLog(@"hi3");
            [_musicPlayer play];
        }
    }
}

What is wrong with it or what is causing this error?

1

There are 1 best solutions below

2
childrenOurFuture On

Check two points which may cause the problem:
One: where is _listOfMusic init?
Two: how do you know [dict objectForKey:@"data"] is a array and has items?