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

查看:229
本文介绍了如何从互联网下载音频/视频文件并存储在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属性:nil error:& error]; //创建文件夹(如果尚不存在)

下载 p>

如果你想从互联网下载音频文件,那么你想看看一个异步的方法来做。这意味着应用程序可以在下载发生在后台执行任务。



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



该网站提供大量关于下载和存储文件的文档。请查看页面上的下载文件部分。



您只需执行以下操作:

  NSURL * url = NSURL URLWithString:@http://www.fileurl.com/example.mp3]; 
ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:[NSString stringWithFormat:@%@,dataPath]]; //使用前面的路径
[request setDelegate:self];
[request startAsynchronous];

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



播放



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



这里有一个伟大的教程使用AVAudioPlayer 播放声音。在你的情况下使用它的一个非常简单的例子是:

  NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:@ %@ / audiofile.mp3,数据路径]; 

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

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


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天全站免登陆