2016-03-31 7 views
-1

Ich habe einen Audio-Player. Ich lade Audio auf diese WeiseWie kann Audio heruntergeladen werden? objective-c

- (void) song{ 
if (_index == 0) { 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"1.mp3"]; 
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:NO]; 

if (!fileExists) { 
    NSString *stringURL = @"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39VHZ1cUZsM3BhQXM"; 
    NSURL *url = [NSURL URLWithString:stringURL]; 
    NSData *urlData = [NSData dataWithContentsOfURL:url]; 
    [urlData writeToFile:filePath atomically:YES]; 
} 

self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil]; 
} 
} 

Aber wenn Audio-Download kann ich nichts tun. Benutzeroberfläche stoppen. Wie kann Audio heruntergeladen und gleichzeitig in der Benutzeroberfläche ausgeführt werden?

+2

Nie Download/Upload von/zum Internet auf dem Haupt-Thread. Verwenden Sie immer einen Hintergrund-Thread. – rmaddy

+0

Wie kann ich es tun? – user

Antwort

0

Geben Sie Ihrer Methode einen Block, der ausgeführt werden soll, und führen Sie den Download-Code dann im Hintergrund aus. Lassen Sie uns sagen, für Ihren Fall die Ausgabe eine lokale Datei mit dem heruntergeladenen Inhalt ist:

- (void)songWithCompletion:(void (^)(NSString *))completion { 
    NSString *filePath = [self song]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     if (completion) completion(filePath); 
    }); 
} 

Ändern der song Methode filePath zurückzukehren. Nennen Sie es niemals direkt, nur über songWithCompletion.

- (void)song { 
    // ... your code from the OP 
    // don't allocate the audio player here, just return the local file 
    return filePath; 
} 

Nennen Sie es, wie dieser ...

[self songWithCompletion:^(NSString *filePath) { 
    if (filePath) { 
     self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil]; 
     // you should really handle audio player error here, too 
    } else { 
     // handle error 
    } 
}]; 
0

Sie laden die Datei im Hauptthread herunter. Sie müssen einen asynchronen Aufruf durchführen, um ein Stoppen der Benutzeroberfläche zu vermeiden.

0
-(void)downloadAudio 
{ 
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://drive.google.com/uc?export=download&id=0B6zMam2kAK39VHZ1cUZsM3BhQXM"] 
              cachePolicy:NSURLRequestUseProtocolCachePolicy 
             timeoutInterval:60.0]; 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

if (theConnection) {   
    receivedData = [NSMutableData data] ; 
} else {NSLog(@"no connection!"); 
} 



- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

NSLog(@"Succeed! Received %d bytes of data",[receivedData length]); 

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
//NSLog(@"%@", [documentPaths objectAtIndex:0]); 
NSString *documentDirectoryPath = [documentPaths objectAtIndex:0]; 
NSString *folderPath = [documentDirectoryPath stringByAppendingPathComponent:@"audioFile.mp3"]; 
[receivedData writeToFile:folderPath atomically:YES]; 
    NSURL *soundURL = [NSURL fileURLWithPath:folderPath]; 
NSError *error; 
if ([[NSFileManager defaultManager] fileExistsAtPath:folderPath]) { 

    player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundURL error:&error]; 
    player.volume=0.5; 
    NSError *error = nil; 
    if (!error) { 
     [player play]; 
     NSLog(@"File is playing!"); 
    } 
    else{ 
     NSLog(@"Error in creating audio player:%@",[error description]); 
    } 
} 
else{ 
    NSLog(@"File doesn't exist"); 
} 

} 
Verwandte Themen