【问题标题】:Creating an MJPEG Viewer Iphone创建 MJPEG 查看器 Iphone
【发布时间】:2010-01-12 22:32:00
【问题描述】:

我正在尝试在 Objective C 中制作一个 MJPEG 查看器,但我遇到了很多问题。

首先,我使用的是 AsyncSocket(http://code.google.com/p/cocoaasyncsocket/),它可以让我连接到主机。

这是我目前得到的结果

NSLog(@"Ready");
asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
//http://kamera5.vfp.slu.se/axis-cgi/mjpg/video.cgi
NSError *err = nil;
if(![asyncSocket connectToHost:@"kamera5.vfp.slu.se" onPort:80 error:&err])
{
    NSLog(@"Error: %@", err);
}

然后在 didConnectToHost 方法中:

 - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
NSLog(@"Accepted client %@:%hu", host, port);


NSString *urlString = [NSString stringWithFormat:@"http://kamera5.vfp.slu.se/axis-cgi/mjpg/video.cgi"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];

    //set headers
NSString *_host = [NSString stringWithFormat:host];
[request addValue:_host forHTTPHeaderField: @"Host"];

NSString *KeepAlive = [NSString stringWithFormat:@"300"];
[request addValue:KeepAlive forHTTPHeaderField: @"Keep-Alive"];

NSString *connection = [NSString stringWithFormat:@"keep-alive"];
[request addValue:connection forHTTPHeaderField: @"Connection"];


//get response
NSHTTPURLResponse* urlResponse = nil;  
NSError *error = [[NSError alloc] init];  
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 

NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
    NSLog(@"Response: %@", result);
            //here you get the response
}

}

这会调用 MJPEG 流,但不会调用它来获取更多数据。我认为它所做的只是加载第一块数据,然后断开连接。

我这样做是完全错误的还是这条隧道的尽头有光?

谢谢!

【问题讨论】:

  • 谁能帮忙???请……

标签: iphone objective-c asyncsocket mjpeg


【解决方案1】:

尝试在 UiWebView 中加载 mjpeg,它应该可以原生播放。

假设你有一个名为“myWebView”的 UiWebView,这样的事情应该可以工作:

NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://kamera5.vfp.slu.se/axis-cgi/mjpg/video.cgi"]];
[myWebView loadRequest:urlRequest];

希望对你有帮助!

【讨论】:

  • 我想我可以做到。我只是认为有更好的方法来做到这一点。谢谢!
  • IMO 当最简单的方法可以完成工作时,让你的生活复杂化是没有用的。 ;-)
【解决方案2】:

主要问题是webkit从不释放数据,所以过了一会儿它就爆炸了。

【讨论】:

  • 有任何证据吗?用 5 mjpeg 进行了测试,内存使用量在 30 分钟内没有增长
【解决方案3】:

这可能最好使用 JavaScript 来完成,因为没有与 UIWebView 通信的好方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多