UIwebview 文件的下载与保存,以及mp3文件的播放

  1. 这里只是说说异步 单线程下载与文件的保存  
  2. 以下载一个mp3文件并保存为例:  
  3.   
  4. -(void)loading  
  5. {  
  6.     //设置文件下载地址   
  7.     NSString *urlString = [NSString stringWithFormat:@"http://zhangmenshiting2.baidu.com/data2/music/14893666/14893666.mp3?xcode=f7e142418de081ff52f81344843b869a&mid=0.73830637514858"];//这里设置的是一个mp3的下载地址  
  8.     NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)urlString, NULL, NULL,  kCFStringEncodingUTF8 );  
  9.     NSURL *url =[NSURL URLWithString:encodedString];  
  10.       
  11.     //创建NSURLRequest和NSURLConnection,并立即启动连接  
  12.     NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5.0f];  
  13.     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];  
  14.    if (connection)   
  15.     {              
  16.           self.receivedData = [NSMutableData data];//初始化接收数据的缓存  
  17.     }   
  18.      else   
  19.      {  
  20.                       NSLog(@"Bad Connection!");  
  21.       }  
  22.   
  23.     [request release];  
  24.     [connection release];  
  25. }  
  26. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  
  27. {  
  28.     [receivedData setLength:0];//置空数据  
  29.     long long mp3Size = [response expectedContentLength];//获取要下载的文件的长度  
  30.     NSLog(@"%lld",mp3Size);  
  31.   
  32.  }  
  33.   
  34. //接收NSMutableData数据  
  35. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data   
  36. {  
  37.     [receivedData appendData:data];  
  38. }  
  39.   
  40. //接收完毕  
  41. - (void)connectionDidFinishLoading:(NSURLConnection *)connection   
  42. {  
  43.     [connection cancel];  
  44.  //在保存文件和播放文件之前可以做一些判断,保证程序的健壮行:例如:文件是否存在,接收的数据是否完整等处理,此处没加,使用时注意  
  45.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);      
  46.     NSString *documentsDirectory = [paths objectAtIndex:0];  
  47.     NSLog(@"mp3 path=%@",documentsDirectory);      
  48.     NSString *filePath = [documentsDirectory stringByAppendingPathComponent: mp3Name];//mp3Name:你要保存的文件名称,包括文件类型。如果你知道文件类型的话,可以指定文件类型;如果事先不知道文件类型,可以在- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response中获取要下载的文件类型  
  49.       
  50.     //在document下创建文件  
  51.     NSFileManager *fileManager = [NSFileManager defaultManager];   
  52.     [fileManager createFileAtPath:filePath contents:nil attributes:nil];  
  53.     NSLog(@"mp3 path=%@",filePath);  
  54.     //将下载的数据,写入文件中  
  55.     [receivedData writeToFile:filePath atomically:YES];  
  56.   
  57.    //播放下载下来的mp3文件  
  58.     [self playVoice:filePath];  
  59.   
  60.      
  61.   //如果下载的是图片则可以用下面的方法生成图片并显示 create image from data and set it to ImageView  
  62.    /* 
  63.      UIImage *image = [[UIImage alloc] initWithData:recvData]; 
  64.     [imgView setImage:image]; 
  65.     */  
  66. }     
  67.   
  68. 简单的播放mp3文件的方法:  
  69. 使用前要添加库:AudioToolbox.framework和AVFoundation.framework,  
  70. //添加头文件  
  71. #import <AVFoundation/AVFoundation.h>  
  72. #import <AudioToolbox/AudioToolbox.h>  
  73. -(void)playVoice:(NSString *)filePath  
  74. {  
  75.     //播放文件的路径  
  76.     NSURL * musicURL= [[NSURL alloc] initFileURLWithPath:filePath];    
  77.   
  78.     //创建音频 播放器  
  79.     AVAudioPlayer * voicePlayer  = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];  
  80.     self.thePlayer = voicePlayer;  
  81.     [voicePlayer release];  
  82.     
  83.     [musicURL release];  
  84.     [thePlayer setVolume:1];   //设置音量大小  
  85.     thePlayer.numberOfLoops = -1;//设置音乐播放次数  -1为一直循环  
  86.       
  87.    //播放mp3,如果想要实现一些别的功能,可以看看AVAudioPlayer这个类,这里只是实现播放功能  
  88.      [thePlayer play];  
  89. }  
posted @ 2014-12-02 14:28  郑文亮  阅读(1894)  评论(0编辑  收藏  举报