如何从互联网下载音频/视频文件并存储在 iPhone 应用程序中? [英] How to download audio/video files from internet and store in iPhone app?

查看:43
本文介绍了如何从互联网下载音频/视频文件并存储在 iPhone 应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款用于音乐的 iPhone 应用.我想为用户提供一些选项,以便他们可以通过流媒体收听歌曲/音乐,或者他们可以在应用中下载音乐.我知道如何以编程方式在应用程序中流式传输音频文件.但是,我不知道如何下载应用程序中的音频文件并在下载后播放音频.并且用户还可以在下载时暂停下载.有可能吗?任何人都可以指导我并为我分享任何示例代码或教程吗?我还有一个疑问:你能帮我在应用程序内创建一个文件夹吗?请帮我.提前致谢.

解决方案

创建文件夹

对于每个应用程序,您都有一个 文档文件夹.您在应用中使用的任何文件都存储在此处.如果您想在此处创建更多目录,则可以执行以下操作:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];//获取文档文件夹NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyNewFolder"];if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];//如果文件夹不存在则创建

下载

如果您想从 Internet 下载音频文件,那么您需要查看执行此操作的异步方法.这意味着当您的下载在后台进行时,应用程序可以继续执行操作.

为此,我建议您使用 ASIHTTPREQUEST.它非常适合执行任何下载请求,为您提供许多选项,例如暂停下载(根据您的要求).

该网站提供了大量有关下载和存储文件的文档.查看页面上的下载文件部分.>

您只需执行以下操作:

NSURL *url = [NSURL URLWithString:@"http://www.fileurl.com/example.mp3"];ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[请求 setDownloadDestinationPath:[NSString stringWithFormat:@"%@",dataPath]];//使用之前的路径[请求 setDelegate:self];[请求开始异步];

然后只需通过 ASIHTTPRequest 提供的委托方法处理进度和完成.

播放

播放文件非常简单.一旦它在设备上并且您知道它在哪里,只需将其加载到音频播放器中.

这里有关于使用 AVAudioPlayer 播放声音的很棒的教程.在您的情况下使用它的一个非常简单的例子是:

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3",dataPath];NSError *错误;audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];如果(音频播放器 == 零)NSLog([错误描述]);别的[音频播放器播放];

I am developing an iPhone app for music. I want to give some options to the user so they can listen the song/music by streaming or they can download the music in the app. I know how to stream the audio files in the app programmatically. But, i don't know how to download the audio files in the app and play the audio after download. And also user can pause the download while it is in download. Is it possible to do ? Can anyone please guide me and please share any sample code or tutorial for me? I have one more doubt: Can you please help me to create a folder inside of the app? Please help me. Thanks in advance.

解决方案

Creating a Folder

For every app you have a Documents Folder. Any files you use in your app are stored here. If you want to create more directories here, then you'd do something like this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyNewFolder"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder if it doesn't already exist

Downloading

If you want to download audio files from the internet, then you want to look at an asynchronous method of doing it. This means that the application can carry on doing things while your download happens in the background.

For this, I recommend you use ASIHTTPREQUEST. It's great for carrying out any download requests, providing you with many options such as pausing a download (as you requested).

The website provides a great deal of documentation on downloading and storing a file. Take a look at the Downloading a File section on this page.

You'd just do something like:

NSURL *url = [NSURL URLWithString:@"http://www.fileurl.com/example.mp3"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:[NSString stringWithFormat:@"%@",dataPath]]; //use the path from earlier
[request setDelegate:self];
[request startAsynchronous];

Then just handle the progress and completion through the delegate methods that ASIHTTPRequest offers.

Playing

Playing the file is very simple. Once it's on the device and you know where it is, just load it into the audio player.

There's a great tutorial here on using AVAudioPlayer to play sounds. A very simple example of using it in your case would be:

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3",dataPath];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

if (audioPlayer == nil)
    NSLog([error description]);
else
    [audioPlayer play];

这篇关于如何从互联网下载音频/视频文件并存储在 iPhone 应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆