2016-03-31 9 views
-1

私はオーディオプレーヤーを持っています。このようにしてオーディオをダウンロードします。どのようにオーディオをダウンロードできますか? 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]; 
} 
} 

しかし、私は何もできません。ユーザーインターフェイスの停止。 オーディオをダウンロードしてユーザーインターフェイスで何かを同時に行うにはどうすればいいですか?

+2

このようにそれを呼び出します。必ずバックグラウンドスレッドを使用してください。 – rmaddy

+0

どうすればいいですか? – user

答えて

0

終了時にブロックを実行し、ダウンロードコードをバックグラウンドで実行します。

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

変更filePathを返すようにsong方法:あなたのケースのための出力がダウンロードされたコンテンツとローカルファイルである、と言うことができます。 songWithCompletion経由でしか直接呼び出さないでください。

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

は、メインスレッド上でインターネットに/からアップロード/ダウンロードすることはありません...

[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

メインスレッドでファイルをダウンロードしています。 UIの停止を避けるために非同期呼び出しを行う必要があります。

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

} 
関連する問題