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?
Check two points which may cause the problem:
One: where is
_listOfMusicinit?Two: how do you know
[dict objectForKey:@"data"]is a array and has items?