2010-01-12 62 views
0

我想在目標C中創建一個MJPEG查看器,但我遇到了一堆問題。創建一個MJPEG查看器iphone

首先,我使用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流,但它不會調用它來獲得更多數據。我認爲它所做的只是加載第一部分數據,然後斷開連接。

我這樣做完全錯誤還是在隧道盡頭有光?

謝謝!

+0

任何人都可以幫忙嗎?請...... – Tony 2010-02-08 16:37:47

回答

1

嘗試在UiWebView中加載mjpeg,它應該能夠本地播放它。

假設你有一個名爲 「myWebView」 UIWebView中,這樣的事情應該工作:

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

我希望幫助!

+0

我想我可以做到這一點。我只是覺得有一個更好的方法來做到這一點。謝謝! – Tony 2010-02-15 21:24:38

+0

當最簡單的方式可能完成工作時,IMO沒有任何使你的生活複雜化的用處。 ;-) – Skrud 2010-02-16 00:37:37

0

這可能是最好的JavaScript處理,因爲沒有好的方法與UIWebView進行通信。

1

主要的問題是webkit從來沒有放棄數據,所以過了一段時間它就爆炸了。

+0

有什麼證明呢?用5 mjpegs做了測試,內存使用量在30分鐘內沒有增長 – thedanielhanke 2011-05-11 15:08:36