tabBar应用程序中下载多首歌曲的问题

发布于 2024-12-02 09:09:16 字数 307 浏览 0 评论 0原文

我有选项卡栏应用程序,在第一个选项卡中我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我为君王 2024-12-09 09:09:16

ASINetworkQueue 只是 NSOperationQueue 的子类,它的作用是创建一个 Request 对象队列,因此,例如,您可以有 10 个请求等待,并在以下时间参加 5 个请求:有时,当一个请求完成时,队列中的另一个请求将变为活动状态。

拥有一个请求队列对于您的情况当然很有帮助,但是您还没有粘贴任何关于您现在如何处理请求的代码。所以我会给你一个关于如何完成的“一般概念”:

首先,我假设你已经弄清楚如何识别用户何时要下载歌曲,并拥有文件的 URL 。如果没有,这是另一个相关问题。另外,安装了 ASI

让我们添加一个处理下载的对象,例如 DownloadManager:

#import "ASINetworkQueue.h"
#import "ASIHTTPRequest.h"

@interface DownloadManager : NSObject <ASIHTTPRequestDelegate>
{
    ASINetworkQueue *requestQueue;
}

+ (DownloadManager*)sharedInstance;
- (void)addDownloadRequest:(NSString*)URLString;

我将使此类表现得像单例 (基于这个答案),因为我想您正在使用单个下载队列。如果不是这种情况,请根据您的需求进行调整:

@implementation DownloadManager

static DownloadManager *_shared_instance_download_manager = nil;

+ (DownloadManager*)sharedInstance
{
    @synchronize
    {
        if (!_shared_instance_download_manager)
        {
            _shared_instance_download_manager = [[DownloadManager alloc] init];
        }

        return _shared_instance_download_manager
    }
}

- (id)init
{
    self = [super init];

    if (self)
    {
        requestQueue = [[ASINetworkQueue alloc] init];
        requestQueue.maxConcurrentOperationCount = 2;
        requestQueue.shouldCancelAllRequestsOnFailure = NO;
    }

    return self;
}

- (void)addDownloadRequest:(NSString*)URLString
{
    NSURL *url = [NSURL URLWithString:URLString];

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.delegate = self;

    [requestQueue addOperation:request];
    [requestQueue go];
}

- (void)requestFinished:(ASIHTTPRequest*)request
{
    // Request finished.
}

- (void)dealloc
{
    [requestQueue release];

    [super dealloc];
}

@end

完成所有操作后,现在您可以添加下载请求,只需执行以下操作:

DownloadManager *manager = [DownloadManager sharedInstance];
[manager addDownloadRequest:MyUrl];

第一个选项卡将项目添加到 DownloadManager,另一个选项卡必须听到下载何时完成或当前状态。我没有将其添加到代码中,因为它取决于您如何做这些事情。它可以是自定义委托方法(即 - (void)DownloadManager:(DownloadManager*)downloadManager didFinishDownloadRequest:(ASIHTTPRequest*)request),或传递请求的当前委托,或使用 NSNotificationCenter

The ASINetworkQueue is just a subclass of NSOperationQueue, 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:

#import "ASINetworkQueue.h"
#import "ASIHTTPRequest.h"

@interface DownloadManager : NSObject <ASIHTTPRequestDelegate>
{
    ASINetworkQueue *requestQueue;
}

+ (DownloadManager*)sharedInstance;
- (void)addDownloadRequest:(NSString*)URLString;

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:

@implementation DownloadManager

static DownloadManager *_shared_instance_download_manager = nil;

+ (DownloadManager*)sharedInstance
{
    @synchronize
    {
        if (!_shared_instance_download_manager)
        {
            _shared_instance_download_manager = [[DownloadManager alloc] init];
        }

        return _shared_instance_download_manager
    }
}

- (id)init
{
    self = [super init];

    if (self)
    {
        requestQueue = [[ASINetworkQueue alloc] init];
        requestQueue.maxConcurrentOperationCount = 2;
        requestQueue.shouldCancelAllRequestsOnFailure = NO;
    }

    return self;
}

- (void)addDownloadRequest:(NSString*)URLString
{
    NSURL *url = [NSURL URLWithString:URLString];

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    request.delegate = self;

    [requestQueue addOperation:request];
    [requestQueue go];
}

- (void)requestFinished:(ASIHTTPRequest*)request
{
    // Request finished.
}

- (void)dealloc
{
    [requestQueue release];

    [super dealloc];
}

@end

With all that done, now you can then add the download requests simply doing:

DownloadManager *manager = [DownloadManager sharedInstance];
[manager addDownloadRequest:MyUrl];

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.

煞人兵器 2024-12-09 09:09:16

我会以不同的方式建议它。

尝试将歌曲下载的代码放在不同的类中。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文