0

我正在尝试在 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 流,但不会调用它来获取更多数据。我认为它所做的只是加载第一块数据,然后断开连接。

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

谢谢!

4

3 回答 3

1

Try loading the mjpeg in a UiWebView, it should be able to play it natively.

Assuming you have a UiWebView called "myWebView", something like this should work:

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

I hope that helps!

于 2010-02-09T21:05:37.197 回答
1

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

于 2010-08-16T11:20:42.403 回答
0

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

于 2010-07-08T15:27:14.313 回答