-
UIwebview 文件的下载与保存,以及mp3文件的播放
- 这里只是说说异步 单线程下载与文件的保存
- 以下载一个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 *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);
-
- }
-
- - (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];
-
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- [fileManager createFileAtPath:filePath contents:nil attributes:nil];
- NSLog(@"mp3 path=%@",filePath);
-
- [receivedData writeToFile:filePath atomically:YES];
-
-
- [self playVoice:filePath];
-
-
-
-
- }
-
- 简单的播放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;
-
-
- [thePlayer play];
- }
-
相关阅读:
Debug入门之旅StackoverFlow exception的调试
[软件调试学习笔记]WinDbg演示IA32 CPU下的Windows 分页机制下的地址转换过程
转载推荐:COM中不同字符类型相互转换,例如char*, BSTR, CString等等
一种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)
COM中的error handling机制及示例(ISupportEfforInfo,ICreateErrorInfo,IErrorInfo)
(转载)如何在WCF实现impersonnate客户端的功能
如何实现DCOM或者COM+的远程调用
[软件调试学习笔记]防止栈缓冲区溢出的基于Cookie的安全检查机制
关于COM的RegFree(免注册)技术简介及实例讲解。
如何通过扩展WCF来定制和扩展WCF行为
-
原文地址:https://www.cnblogs.com/zhwl/p/4137346.html
Copyright © 2020-2023
润新知