如何使用从 Socket 连接加载的数据初始化视频对象 - iOS

发布于 2024-12-04 21:39:22 字数 1734 浏览 1 评论 0原文

我正在尝试在 iOS 上通过网络传输视频 (H264)。但是,我通过远程服务器的打开套接字(使用 serverSocket)将视频数据(NSData)放入缓冲区,因此我没有可用于创建 MPMoviePlayerController 的视频 URL。 如何以这种方式初始化并在 iPhone/iPad 上播放视频?

这是来自客户端的部分代码:

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

问题是:我可以从内容(NSData)获取数据,这意味着服务器端没问题。但我不知道如何将 NSData 中的数据提取到 NSString 中,然后提取到 NSURL 中,以便最终可以被 MPMoviePlayerController 使用。

任何帮助将不胜感激。 预先非常感谢您!

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 server 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清泪尽 2024-12-11 21:39:22

看起来你正在同步下载文件然后播放,恕我直言,这是一种可怕的方法,但如果这就是你想要的..

一旦你有了 NSData 的路径(存储到带有可识别视频扩展名的磁盘),你就可以就这样做:

myvideoPlayer = [[MPMoviePlayerController 分配]
initWithContentURL:[NSURL URLWithString:path]];

调试错误信息根本没有帮助,而且很容易因为简单的错误而浪费时间,因此请注意文件名和类型。

沿着更好的用户体验(异步)的路线:

或者您可以流式传输。也可以同时进行流式传输并保存到磁盘,但这并不是特别容易,因为您必须自己处理 eof - 有点有趣,因为 MPMoviePlayerController 没有任何 eof 事件。我应该说,当文件仍在开箱即用地下载时,可以播放 mp4,但进行一些 cleaver 文件创建(创建有效的文件页脚)时,可以对其他文件执行此操作。欢迎提问。

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.

陪你到最终 2024-12-11 21:39:22

无法使用 MPMoviePlayerController - 没有用于使用 NSData 代替 URL(本地或远程)的公共接口。

Not possible using MPMoviePlayerController - there is no public interface for using NSData instead of a URL (local or remote).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文