Each row of the table will contain a song. When a row is selected. The current streams are closed and new streams are created for the same peers and sent. In the recieving side i detect the duplicate streams from the same peer and replace them in an array that i used to store the input streams. But the problem is the receivers are not getting the second stream that am sending
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
for(AudioOutputStream *i in [ConnectionAndStreams outputStreams]){
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[i stop];
}];
}
NSLog(@"Stoped");
[ConnectionAndStreams clearOutputStreams];
for(MCPeerID *i in [ConnectionAndStreams outputSession].connectedPeers){
AudioOutputStream *temp = [[AudioOutputStream alloc]initWithOutputStream:[[ConnectionAndStreams outputSession] outputStreamForPeer:i]];
[ConnectionAndStreams addOutputStream:temp];
[ConnectionAndStreams setCurrentSong:[ConnectionAndStreams outputSongs][indexPath.row] forOutputStream:temp];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"title"] = [ConnectionAndStreams currentSongforOutputStream:temp].title;
UIImage *albumPicImage = [[ConnectionAndStreams currentSongforOutputStream:temp].artwork imageWithSize:CGSizeMake(500, 500)];
dict[@"image"] = albumPicImage;
NSArray *tempArr = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[ConnectionAndStreams outputSession].getDisplayName,dict,nil];
[[ConnectionAndStreams outputSession]sendData:[NSKeyedArchiver archivedDataWithRootObject:tempArr] toPeer:i];
[temp streamAudioFromUrl:[[ConnectionAndStreams currentSongforOutputStream:temp] valueForProperty:MPMediaItemPropertyAssetURL]];
[temp start];
}
}
This is the code for the initWithOutputStream method
-(id)initWithOutputStream:(NSOutputStream *)stream{
self = [super init];
if(!self) return nil;
self.audioStream = [[AudioStream alloc]initWithOutputStream:stream];
self.audioStream.delegate = self;//To handle the stream events
return self;
}
And the code for the outputStreamForPeer
-(NSOutputStream *)outputStreamForPeer:(MCPeerID *)peerId{
NSError *error;
NSOutputStream *stream = [self.session startStreamWithName:@"Music" toPeer:peerId error:&error];
if(error)
NSLog(@"The error is %@",[error userInfo].description);
return stream;
}