tabBar应用程序中下载多首歌曲的问题
我有选项卡栏应用程序,在第一个选项卡中我有一个 webView,因此当用户打开网站并要下载一些歌曲 mp3 格式时,它会推送另一个视图,该视图从用户那里获取标题。
给出标题后,我只需将 tabbarSelectController 更改为歌曲的一个标题 下载开始并显示在选项卡栏上选定的索引 1 处。当我将选项卡更改为选定的索引 0 并选择另一首歌曲进行下载并再次返回到选定的索引 1 时,第一次下载停止,第二次下载恢复。
所以我想下载多首歌曲,但不太知道在这种情况下如何做到这一点,因为用户动态添加歌曲我也看到了 ASINtworkQueue,但不知道它是如何工作的
I have tabbar application, in first tab I have a webView, so when user open the website and going to download some song mp3 format, it push another View which takes the title from the user.
after giving the title I just change the tabbarSelectController to one title for song
and the downloading begins and shown on the tabbar at selected Index 1. when i change the tab to selected Index 0 and select another song for downloading and again back to selectedIndex 1 first downloading stop and 2nd downloading resume.
so i want to download multiple songs and don't have much idea how to do that in this scenario because user add songs dynamically I have seen ASINtworkQueue as well but don't get the idea of how that works
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ASINetworkQueue
只是 NSOperationQueue 的子类,它的作用是创建一个 Request 对象队列,因此,例如,您可以有 10 个请求等待,并在以下时间参加 5 个请求:有时,当一个请求完成时,队列中的另一个请求将变为活动状态。拥有一个请求队列对于您的情况当然很有帮助,但是您还没有粘贴任何关于您现在如何处理请求的代码。所以我会给你一个关于如何完成的“一般概念”:
首先,我假设你已经弄清楚如何识别用户何时要下载歌曲,并拥有文件的 URL 。如果没有,这是另一个相关问题。另外,安装了 ASI。
让我们添加一个处理下载的对象,例如 DownloadManager:
我将使此类表现得像单例 (基于这个答案),因为我想您正在使用单个下载队列。如果不是这种情况,请根据您的需求进行调整:
完成所有操作后,现在您可以添加下载请求,只需执行以下操作:
第一个选项卡将项目添加到
DownloadManager
,另一个选项卡必须听到下载何时完成或当前状态。我没有将其添加到代码中,因为它取决于您如何做这些事情。它可以是自定义委托方法(即- (void)DownloadManager:(DownloadManager*)downloadManager didFinishDownloadRequest:(ASIHTTPRequest*)request
),或传递请求的当前委托,或使用 NSNotificationCenter。The
ASINetworkQueue
is just a subclass ofNSOperationQueue
, what it does, is create a queue of Request objects, so for example, you can have 10 requests waiting, and attend 5 at a time, when a request is completed, another request in the queue becomes active.Having a queue of requests is certainly helpful in your case, but you haven't pasted any code, on how you're dealing with the requests right now. So I'm gonna give you a "general concept" on how it should be done:
First, I'm assuming that you already figured out how to identify when the user is going to download the song, and have the URL of the file. If not, here's another question related. Also, have ASI installed.
Let's add an object which handles the downloads, say, DownloadManager:
I will make this class behave like a singleton (based on this answer), because I imagine that you're working with a single download queue. If this is not the case, adapt it to your needs:
With all that done, now you can then add the download requests simply doing:
The first tab would add the items to the
DownloadManager
, the other tab would have to hear for when a download is complete, or the current status. I didn't add that into the code, as it's dependant on how you do these things. It could be a custom delegate method (i.e- (void)DownloadManager:(DownloadManager*)downloadManager didFinishDownloadRequest:(ASIHTTPRequest*)request
), or pass the current delegate of the requests, or using NSNotificationCenter.我会以不同的方式建议它。
尝试将歌曲下载的代码放在不同的类中。 Iphone OS 会自动为该类的每个对象启动一个新连接。现在两个下载可以同时继续。
它工作得很漂亮。我已经看到了。
I will suggest it different way..
Try putting a code for song download in different class. Iphone O.S. will automatically start a new connection for each object of that class. Now both downloads can continue at same time.
It works beautyfully. I have seen it.