how to initialize a video object using the data loaded from a Socket connection - iOS
I'm trying to stream a video (H264) across a network on iOS. However, I'm getting the video data(NSData) into a buffer through an open socket to the remote server (using serverSocket), so I don't have a URL to the video that I can use to create an MPMoviePlayerController. How can I initialize and play the video on my iPhone/iPad in this way?
Here is the part of the code from the client side:
NSString *requestString = textField_.text;
NSString *url = @"http://192.168.3.11:10000";
NSData *myRequestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:myRequestData];
NSURLResponse *response = nil;
NSError *error = nil;
//send the request and get the response from the server.
NSData *content = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
#pragma mark--
#pragma mark--test video
//parse the content(NSData) into NSString.
NSString *string = [[NSString alloc] initWithData:content encoding:NSASCIIStringEncoding];
//and then into NSURL.
NSURL *url2 = [NSURL URLWithString:string];
//so that it can be used by MPMoivePlayerController.
player_ = [[MPMoviePlayerController alloc] initWithContentURL:url2];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:player_];
player_.shouldAutoplay = NO;
player_.repeatMode = YES;
player_.view.frame = CGRectMake(10, 10, 300, 300);
[self.view addSubview:player_.view];
[player_ play];
The problem is: I can get data from the content(NSData),which means the s开发者_如何学JAVAerver side is ok. But I don't know how to extract the data from NSData into NSString and then into NSURL so that eventually it could be used by MPMoviePlayerController.
Any help would be appreciated. Thank you very much in advance!
It looks like you are synchronously downloading the file then playing, this is a horrible way to to do it imho but if thats what you want..
Once you have a path to your NSData (stored to disk with a recognisable video extension) you can just do this:
myvideoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:path]];
Debugging info for errors is not helpful at all and hours can easily be lost to simple mistakes, so be diligent with your filenames and types.
Going down the route of a better user experience (async):
Alternatively you can stream. It is also possible to stream and save to disk at the same time but it's not particularly easy as you have to handle eofs yourself - kind of funny because MPMoviePlayerController does not have any eof events. I should say that its possible to play mp4's while the file is still downloading fairly out of the box but doing some cleaver file creation (creating a valid file footer) it is possible to do this with other files. Feel free to ask questions.
Not possible using MPMoviePlayerController
- there is no public interface for using NSData
instead of a URL (local or remote).
精彩评论