ios 如何从互联网下载音频/视频文件并存储在 iPhone 应用程序中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10245345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:58:21  来源:igfitidea点击:

How to download audio/video files from internet and store in iPhone app?

iphoneiosdownloadaudio-streamingaudio-player

提问by Gopinath

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 streamingor 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 playthe 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.

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

回答by Liam George Betsworth

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:

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

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.

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

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

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

The website provides a great deal of documentation on downloading and storing a file. Take a look at the Downloading a Filesection on thispage.

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

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.

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

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 AVAudioPlayerto play sounds. A very simple example of using it in your case would be:

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

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

回答by Khalid Usman

Yes, you can download audio files and play downloaded files through AVAudioPlayer. I don't exactly know how to pause when downloading, but I have downloaded audios and played. You can download either from ASIHTTP Request or other.

是的,您可以通过 AVAudioPlayer 下载音频文件和播放下载的文件。我不知道如何在下载时暂停,但我已经下载了音频并播放了。您可以从 ASIHTTP 请求或其他下载。

You can follow these links.

您可以点击这些链接。

  1. http://allseeing-i.com/ASIHTTPRequest/How-to-use#tracking_progress
  2. About download file using ASIHttpRequest
  1. http://allseeing-i.com/ASIHTTPRequest/How-to-use#tracking_progress
  2. 关于使用 ASIHttpRequest 下载文件