2

我有一个从服务器播放音频文件 (mp3) 的 iphone 应用程序,我想添加一个下载按钮,以便用户可以将这些文件下载到应用程序中。因此,他/她每次想要收听这些文件时都不需要互联网连接。有没有人有一个示例代码?

我在网上看到了很多东西,但我仍然感到困惑。这是我正在使用的代码:

在 .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];

}

我不知道是什么问题或我在这里遗漏了什么,你能帮我提供更多细节吗?提前致谢。

4

2 回答 2

2

使用 ASIHTTP 进行下载,它是异步的,更可靠。http://allseeing-i.com/ASIHTTPRequest/ 这是您可以下载音频/视频的方法。将参数传递给它并开始下载?

参数:(1) File Path ,要保存的文档目录路径 (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];
}

希望这会帮助你。如果仍然有问题,请给我发电子邮件,我相信我会这样做,因为我已经在我的两个应用程序中实现了它。

于 2012-11-10T11:58:41.770 回答
0

试试下面的代码:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
[request setDownloadDestinationPath:destinationPath];   
[request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"%@-part",destinationPath]];
[request setDelegate:self];
[request setAllowResumeForFileDownloads:YES];
[request startAsynchronous];

并检查下载:

- (void)requestStarted:(ASIHTTPRequest *)request;
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders;
- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL;
- (void)requestFinished:(ASIHTTPRequest *)request;
- (void)requestFailed:(ASIHTTPRequest *)request; 

上面的代码将在下载过程中在目标文件夹中创建一个文件“your_file.mp3-part”,在下载完成时创建一个“your_file.mp3”。

希望能帮助到你。如果有任何疑问,请告诉我。

于 2012-11-19T12:12:42.247 回答