How to send audio stream using UDP socket in ios sdk

How do you send audio and video stream data over UDP sockets. Also, how do you stream the audio file from the server.

I am using this project: https://github.com/robbiehanson/CocoaAsyncSocket

but some data doesn't send how to solve this problem.

send nsdata code:

  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);

      

and get the code there:

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

}

}

      

+3
ios iphone audio-streaming


source to share


No one has answered this question yet

Check out similar questions:

512
How to programmatically send SMS to iPhone?
79
Streaming audio from a URL in Android using MediaPlayer?
25
Streaming mp3 audio with AVPlayer
8
Base64 Encode File Using NSData Blocks
6
Audio Streaming with AVPlayer
6
GCD and KVO problems
4
How to search for streaming audio on iPhone?
3
Streaming audio files in iOS
2
iPhone - Search in Streaming Audio
1
Microphone UDP Direct Stream Audio



All Articles
Loading...
X
Show
Funny
Dev
Pics