如何通过UDP套接字发送音频和视频流数据。另外,如何从服务器流式传输音频文件。
我正在使用这个项目:https://github.com/robbiehanson/CocoaAsyncSocket
但是有些数据没有发送如何解决这个问题
发送nsdata代码:
NSString *host = addrField.text;
if ([host length] == 0)
{
[self logError:@"Address required"];
return;
}
int port = [portField.text intValue];
if (port <= 0 || port > 65535)
{
[self logError:@"Valid port required"];
return;
}
NSString *msg = [[NSBundle mainBundle] pathForResource:@"11" ofType:@"mp4"];
if ([msg length] == 0)
{
[self logError:@"Message required"];
return;
}
NSData* datafile =[[NSData alloc]init];
datafile= [NSData dataWithContentsOfFile:msg];
NSUInteger length = [datafile length]; // total size of data
NSLog(@"real data len >>%lu",(unsigned long)length);
NSUInteger chunkSize =9000; // divide data into 10 mb
NSUInteger offset = 0;
countfordatbunch=0;
do {
// get the chunk location
NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
// get the chunk
data = [NSData dataWithBytesNoCopy:(char *)[datafile bytes] + offset
length:thisChunkSize
freeWhenDone:NO];
[outputStream write:[data bytes] maxLength:[data length]];
udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
//omitted error checking
[udpSocket enableBroadcast:YES error:nil];
[udpSocket bindToPort:port error:nil];
[udpSocket joinMulticastGroup:host error:nil];
[udpSocket beginReceiving:nil];
[udpSocket sendData:data toHost:host port:port withTimeout:10 tag:tag];
offset += thisChunkSize;
countfordatbunch++;
} while (offset < length);
msg=[NSString stringWithFormat:@"%d",countfordatbunch];
data = [msg dataUsingEncoding:NSUTF8StringEncoding];
//omitted error checking
[udpSocket enableBroadcast:YES error:nil];
[udpSocket bindToPort:port error:nil];
[udpSocket joinMulticastGroup:host error:nil];
[udpSocket beginReceiving:nil];
// [udpSocket receiveWithTimeout:10 tag:1];
[udpSocket sendData:data toHost:host port:port withTimeout:10 tag:tag];
NSLog(@"countfordatbunch >>>%d",countfordatbunch);
并在那里接收代码:
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
fromAddress:(NSData *)address
withFilterContext:(id)filterContext
{
[arraySoundData addObject:data];
NSLog(@"arraySoundData>>%d",arraySoundData.count);
msg1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
int count=[msg1 integerValue];
NSLog(@"msg_count >>%d",count);
if (((arraySoundData.count==count+1)&&arraySoundData.count!=1)||arraySoundData.count==count)
{
[self logMessage:FORMAT(@"RECV: %@", @"message send")];
sound = [NSMutableData data];
int i;
for (i=0; i<[arraySoundData count]; i++)
{
NSData *soundPacket = [arraySoundData objectAtIndex:i];
[sound appendData:soundPacket];
}
NSLog(@"%@",sound);
NSUInteger length1 = [sound length];
NSLog(@"data len >>%lu",(unsigned long)length1);
NSFileManager *fileManager =[NSFileManager defaultManager];
NSString *fileName = @"file.mp4";
NSLog(@"%@",fileName);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSLog(@"%@",dataPath);
BOOL filecreationSuccess = [fileManager createFileAtPath:dataPath contents:sound attributes:nil];
if(filecreationSuccess == NO)
{
NSLog(@"Failed to create the html file");
}
NSURL *movieUrl = [NSURL fileURLWithPath:dataPath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieUrl];
NSLog(@"%@",moviePlayerController);
[self.view addSubview:moviePlayerController.view];
//moviePlayerController.fullscreen = YES;
moviePlayerController.view.frame = CGRectMake(150, 250, 150, 150);
moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
moviePlayerController.controlStyle=MPMovieControlStyleDefault;
[moviePlayerController play];
[arraySoundData removeAllObjects];
//msg1=@"";
NSLog(@"%lu",(unsigned long)arraySoundData.count);
[self.modalViewController.view removeFromSuperview];
}
}