0

我已将其设置为可以在 iOS 设备之间来回发送音乐的位置,但是每当发送音乐时,接收电话都会收到此错误:

*** 由于未捕获的异常“MPMediaItemCollectionInitException”而终止应用程序,原因:“项目数组不能为空”*** 首先抛出调用堆栈:

这是我得到错误的方法:

-(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];
        }
    }
}

它有什么问题或导致此错误的原因是什么?

4

1 回答 1

0

检查可能导致问题的两点:
一:_listOfMusicinit在哪里?
二:你怎么知道[dict objectForKey:@"data"]是一个数组并且有项?

于 2016-03-14T03:52:37.540 回答