创建MJPEG浏览器iPhone
-
20-09-2019 - |
题
我试图使目标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流,但它不会调用它来获得更多的数据。我认为它做的只是加载数据的第一块,然后断开连接。
我这样做完全错误或在该隧道的端部有亮?
谢谢!
解决方案
尝试在一个UIWebView加载MJPEG,它应该能够本地播放。
假设你有一个名为 “myWebView” UIWebView中,这样的事情应该工作:
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://kamera5.vfp.slu.se/axis-cgi/mjpg/video.cgi"]];
[myWebView loadRequest:urlRequest];
我希望帮助!
其他提示
的主要问题是,WebKit的从未释放数据,所以一段时间后它会爆炸。
这将可能是最好用JavaScript来实现,因为没有与UIWebView的沟通的好办法,否则。
不隶属于 StackOverflow