11

Ich habe einen Musik-Player in iPhone (SDK 4) implementiert und es funktioniert sowohl mit Streaming von Mp3 (Transcoded on the fly) oder rohen progressiven Download.Cache Progressive heruntergeladene Inhalte in MPMoviePlayerController

Gibt es trotzdem, um den progressiv heruntergeladenen Inhalt (den ich nicht direkt kontrolliere) in den Cache zu stellen, damit der gesamte Inhalt nicht erneut heruntergeladen wird?

Oder Gibt es eine globale Cache-Einstellung zur Steuerung? (Nebenbei bemerkt, habe ich ASIHTTP-APIs verwendet, um meinen HTTP-Server zu kontaktieren und auf Daten zuzugreifen, die Caching erlauben).

Vielen Dank im Voraus.

Antwort

1

Sie können NSURLConnection verwenden, um die Datei zu speichern und sie wiederzugeben.

Beispiel:

-(void)downloadFileAtPath:(NSString *)path 
{ 
    NSURL *url     = [NSURL URLWithString:path];  
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 
     NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:req delegate:self]; 
    [connection start]; 
} 

-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response 
{ 
    //NSLog(@"%s", __PRETTY_FUNCTION__); 
    [[NSFileManager defaultManager] createFileAtPath:pathForCurrentFile contents:nil attributes:nil]; 
    currentFile = [NSFileHandle fileHandleForUpdatingAtPath:pathForCurrentFile]; 
    if (currentFile) 
    { 
     [currentFile seekToEndOfFile]; 
    } 
} 

-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data 
{ 
    //NSLog(@"%s", __PRETTY_FUNCTION__); 
    if(currentFile != nil){ 
     if (currentFile) { 
      [currentFile seekToEndOfFile]; 
     } 
     [currentFile writeData:data]; 
    } 
} 

-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error 
{ 
    NSLog(@"%s, %@", __PRETTY_FUNCTION__, error); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [currentFile closeFile]; 
} 

Unten ist der Code für die Wiedergabe

//Playback 
NSString *loopFilePath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:vidFileName]; 

NSURL *vidURL = [NSURL fileURLWithPath:vidFilePath]; 

self.vidController = [[MPMoviePlayerController alloc] initWithContentURL:vidURL]; 
self.vidController.view.frame = CGRectMake(0, 0, 640, 480); 
self.vidController.controlStyle = MPMovieControlStyleDefault; 
[self.view addSubview:self.vidController.view]; 
[self.vidController prepareToPlay]; 
[self.vidController play]; 
+0

Sie funktioniert das getestet? – vaughan

+0

Hallo, haben Sie Erfolg mit dieser Lösung? – Bkillnest

Verwandte Themen