Domanda

Sto cercando di fare un visualizzatore MJPEG in Objective C, ma sto avendo un po 'di problemi con esso.

Prima di tutto, sto usando AsyncSocket ( http://code.google.com/p / cocoaasyncsocket / ) che mi permette di connettersi all'host.

Ecco quello che ho ottenuto finora

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);
}

poi nel metodo 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
}

}

Ciò richiede il flusso MJPEG, ma non chiama per ottenere più dati. Quello che penso il suo fare è solo caricando il primo blocco di dati, quindi scollegare.

sto facendo questo totalmente sbagliato o c'è luce alla fine di questo tunnel?

Grazie!

È stato utile?

Soluzione

Provare a caricare il MJPEG in un UIWebView, dovrebbe essere in grado di riprodurre in modo nativo.

Supponendo di avere un UIWebView chiamato "myWebView", qualcosa di simile a questo dovrebbe funzionare:

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

Mi auguro che aiuta!

Altri suggerimenti

Il problema principale è che webkit mai rilasciare i dati, così dopo un po 'esplodere.

Che sarebbe probabilmente meglio essere fatto con JavaScript dal momento che non v'è un buon modo per comunicare con UIWebView altrimenti.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top