从服务器下载音乐文件并保存在我的应用程序 [英] Download music file from server and save in my app
问题描述
我需要从服务器下载一个.mp3音频文件,以保存在我的应用程序中,并且无需下载,就像下面的屏幕截图一样,下载的进度条(我在App Store中发现了这个)。有谁可以帮助我在我的应用程序,用源代码?
任何帮助将不胜感激!感谢提前!
I need to download an .mp3 audio file from a server to save in my app and play it without downloading, exactly like the screenshot below, with a progress bar for the download (I found this in the App Store). Can anyone help me to do this in my app, with source code? Any help will be appreciated! Thanks in advance!
我使用了 NSURLConnection
,但对我来说并不真正。
I used NSURLConnection
but it does not really work for me.
这是我正在尝试做的截图。
Here is the screenshot of what I am trying to do.
推荐答案
这个的问题讨论你正在寻找的东西
This questions discusses what you're looking for
编辑:
如果要下载文件,请执行以下操作:
If you want to download a file THEN play it, what you need to do is:
使用NSURLConnection将文件下载到磁盘 - 这个链接讨论了保存大文件(我不会保持2-5mb的内存,当我不需要他们甚至认为可能会工作)。
use an NSURLConnection to download the file to the disk - this link discusses saving large files (i wouldn't keep 2-5mb's in the memory when i don't need them even thought it will probably work).
,而不是AVAudioPlayer播放它 - 检查这个 out。
, than an AVAudioPlayer to play it - check this out.
编辑2:
对于取消按钮,实际上应该只是调用[ NSURLConnection取消]方法,并做任何其他完成您的下载类将需要...
For the cancel button, it should practically just call the [NSURLConnection cancel] method and do whatever other finishing up your download class is gonna need...
对于进度条,在NSURLConnectionDelegate方法didReceiveResponse中,您采取NSURLResponse,将其转换为NSHTTPURLResponse,并使用 expectedContentLength 方法。
For the progress bar, in the NSURLConnectionDelegate method didReceiveResponse, you take the NSURLResponse, cast it to a NSHTTPURLResponse, and retrieve the size of the file you're downloading using the expectedContentLength method.
然后在每次调用NSURLConnection时:didRecieveData方法你计算的总数ng字节,使用[数据长度]方法,除以 expectedContentLength 来自先前的URL响应,这是float表示您进入进度视图的< a href =http://developer.apple.com/library/ios/documentation/uikit/reference/UIProgressView_Class/Reference/Reference.html#//apple_ref/occ/instp/UIProgressView/progress =nofollow noreferrer >进度属性
And then in each call to the NSURLConnection:didRecieveData method you count the incoming bytes using [data length] method, divide it by the expectedContentLength from the previous URL response and that's the float the represents your progress that you pass to the progress view's progress property
这篇关于从服务器下载音乐文件并保存在我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!