我有一个 iPhone 应用程序,可以从服务器播放音频文件 (mp3),我想添加一个下载按钮,以便用户可以将这些文件下载到应用程序中。因此,他/她每次想要收听这些文件时都不需要互联网连接。有人有相关的示例代码吗?
我在网上看到了很多东西,但我仍然很困惑。 这是我正在使用的代码:
在 .h 文件中:
#import
#import "MBProgressHUD.h"
#import
#import
@interface myViewController : UIViewController {
MBProgressHUD *HUD;
long long expectedLength;
long long currentLength;
AVAudioPlayer *theAudio;
}
-(NSString *)pathOfFile;
-(void)applicationWillTerminate:(NSNotification*)notification;
-(IBAction)play:(id)sender;
-(IBAction)download(id)sender;
@property (nonatomic,retain) AVAudioPlayer *theAudio;
在 .m 文件中:
- (IBAction)download:(id)sender {
NSURL *URL = [NSURL URLWithString:@"http://www.server.com/myfile.mp3"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
[connection release];
HUD = [[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] retain];
HUD.labelText = @"Loading...";
}
-(IBAction)play:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [NSString stringWithFormat:@"%@/song.mp3", documentsDirectory];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:file] error:nil];
[theAudio play];
}
-(NSString *)pathOfFile {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingFormat:@"myfile.plist"];
}
-(void)applicationWillTerminate:(NSNotification*)notification {
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:theAudio];
[array writeToFile:[self pathOfFile] atomically:YES];
[array release];
}
我不知道问题出在哪里,或者我在这里缺少什么,您能帮我提供更多详细信息吗? 提前致谢。
最佳答案
使用ASIHTTP下载,异步且更可靠。 http://allseeing-i.com/ASIHTTPRequest/ 这是您可以下载音频/视频的方法。向其传递参数并开始下载?
参数: (1) 文件路径,要保存的文档目录路径 (2) Trimmed String ,即讲座URL
+ (void)downloadingLecture:(NSString *)filePath withLectureUrl:(NSString *)trimmedString
{
AppDelegate *_appDel=(AppDelegate *)[[UIApplication sharedApplication] delegate];
[_appDel.queue setDelegate:self];
[_appDel.queue setMaxConcurrentOperationCount:10];
[_appDel.queue setShouldCancelAllRequestsOnFailure:NO];
[_appDel.queue setShowAccurateProgress:YES];
ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:trimmedString]];
[request setDelegate:self];
[request setDownloadDestinationPath:filePath];
[request setShowAccurateProgress:YES];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setUsername:downloadedSuccessfullString];
[request setDidFinishSelector:@selector(downloadedSuccessfully:)];
[request setDidFailSelector:@selector(downloadedFailure:)];
[_appDel.queue addOperation:request];
[_appDel.queue go];
}
希望这对您有帮助。如果仍然存在问题,请给我发电子邮件,我确信我会这样做,因为我已经在我的两个应用程序中实现了它。
关于iphone - 如何将音频文件从服务器下载到我的 iPhone 应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13315418/