iphone - 如何将音频文件从服务器下载到我的 iPhone 应用程序中?

标签 iphone ios xcode sdk download

我有一个 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/

相关文章:

iphone - 计算不同时区对应的日期和时间

iphone - 如何显示登录和关闭设置 - Storyboard

objective-c - 请解释 NSDateComponents 中按位运算符 OR 的使用?

ios - iOS PhoneGap jQuery 移动应用程序中的可见滚动条

ios - 我的 View Controller 和 Storyboard项目周围不需要的蓝色线条

ios - 解析调用两次的 ObjectsDidLoad

iphone - 将选中的项目保存在 TableView 中,以便以后在 iPhone 中重复使用

ios - 二元运算符 '<<' 不能应用于两个 'T' 操作数

xcode - 使用 Swift 快速查看调试中的 UIImage

swift - macOS Big Sur 新的工具栏按钮样式