標籤:style blog http io ar color os 使用 sp
http://blog.csdn.net/a6472953/article/details/7866033
[cpp] view plaincopy
- 這裡只是說說非同步 單線程下載與檔案的儲存
- 以下載一個mp3檔案並儲存為例:
-
- -(void)loading
- {
- //設定檔案
- NSString *urlString = [NSString stringWithFormat:@"http://zhangmenshiting2.baidu.com/data2/music/14893666/14893666.mp3?xcode=f7e142418de081ff52f81344843b869a&mid=0.73830637514858"];//這裡設定的是一個mp3的
- NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)urlString, NULL, NULL, kCFStringEncodingUTF8 );
- NSURL *url =[NSURL URLWithString:encodedString];
-
- //建立NSURLRequest和NSURLConnection,並立即啟動串連
- NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5.0f];
- NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
- if (connection)
- {
- self.receivedData = [NSMutableData data];//初始化接收資料的緩衝
- }
- else
- {
- NSLog(@"Bad Connection!");
- }
-
- [request release];
- [connection release];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- {
- [receivedData setLength:0];//置空資料
- long long mp3Size = [response expectedContentLength];//擷取要下載的檔案的長度
- NSLog(@"%lld",mp3Size);
-
- }
-
- //接收NSMutableData資料
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- {
- [receivedData appendData:data];
- }
-
- //接收完畢
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
- [connection cancel];
- //在儲存檔案和播放檔案之前可以做一些判斷,保證程式的健壯行:例如:檔案是否存在,接收的資料是否完整等處理,此處沒加,使用時注意
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- NSLog(@"mp3 path=%@",documentsDirectory);
- NSString *filePath = [documentsDirectory stringByAppendingPathComponent: mp3Name];//mp3Name:你要儲存的檔案名稱,包括檔案類型。如果你知道檔案類型的話,可以指定檔案類型;如果事先不知道檔案類型,可以在- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response中擷取要下載的檔案類型
-
- //在document下建立檔案
- NSFileManager *fileManager = [NSFileManager defaultManager];
- [fileManager createFileAtPath:filePath contents:nil attributes:nil];
- NSLog(@"mp3 path=%@",filePath);
- //將下載的資料,寫入檔案中
- [receivedData writeToFile:filePath atomically:YES];
-
- //播放下載下來的mp3檔案
- [self playVoice:filePath];
-
-
- //如果下載的是圖片則可以用下面的方法產生圖片並顯示 create image from data and set it to ImageView
- /*
- UIImage *image = [[UIImage alloc] initWithData:recvData];
- [imgView setImage:image];
- */
- }
-
- 簡單的播放mp3檔案的方法:
- 使用前要添加庫:AudioToolbox.framework和AVFoundation.framework,
- //添加標頭檔
- #import <AVFoundation/AVFoundation.h>
- #import <AudioToolbox/AudioToolbox.h>
- -(void)playVoice:(NSString *)filePath
- {
- //播放檔案的路徑
- NSURL * musicURL= [[NSURL alloc] initFileURLWithPath:filePath];
-
- //建立音頻 播放器
- AVAudioPlayer * voicePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
- self.thePlayer = voicePlayer;
- [voicePlayer release];
-
- [musicURL release];
- [thePlayer setVolume:1]; //設定音量大小
- thePlayer.numberOfLoops = -1;//設定音樂播放次數 -1為一直迴圈
-
- //播放mp3,如果想要實現一些別的功能,可以看看AVAudioPlayer這個類,這裡只是實現播放功能
- [thePlayer play];
- }
UIwebview 檔案的下載與儲存,以及mp3檔案的播放