2015-07-03 110 views
0

我要保存音頻文件的文件夾,同時/及其播放後。緩衝文件格式是mp3。 (更好,如果它可以下載音頻文件的實時而它的播放/緩衝)保存流或緩衝的音頻文件,文檔文件夾

我做搜索了很多在互聯網上,但無法找到的教程或示例代碼專門針對這一要求。

這是怎麼了緩衝的MP3

NSString *strMessageUrl = @"https://example.com/example.mp3"; 

NSLog(@"Url %@", strMessageUrl); 

NSURL *audioUrl = [NSURL URLWithString:strMessageUrl]; 

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
[appDelegate.btnPause setHidden:NO]; 
[appDelegate.btnPlay setHidden:YES]; 
appDelegate.moviePlayer.contentURL=audioUrl; 
appDelegate.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 
[appDelegate.moviePlayer prepareToPlay]; 
[appDelegate.moviePlayer play]; 

感謝。

回答

0

如果是一個URL直接到實際的文件,你可以簡單地創建一個的NSURLRequest和它異步保存到磁盤在您啓動播放器的同時。

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[[NSURL alloc] initWithString:@"https://example.com/example.mp3"]]; 
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] 
         completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
    if (data && connectionError != nil) { 
     NSString *filePath = @"documentDirectory/example.mp3"; 
     if([data writeToFile:filePath atomically:YES]) { 
      //keep track of filePath 
     } 
    } else { 
     NSLog(@"Something went wrong!"); 
    } 
}]; 
+0

我認爲這是行不通的 – Bkillnest