2015-07-03 103 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