0

ASIHTTPRequest を使用して、バックグラウンドで URL からビデオ ファイルをダウンロードしています。

プログレスバーとパーセンテージでダウンロードを表示していますが、ユーザーが一時停止や再開などのダウンロードを制御できるようにしたいと考えています。

以下はコードです:

 -(void)Initiate_Download:(NSString*)urlStr contentID:(NSString*)cid progressBar:(UIProgressView*)progressBar
 {
     NSLog(@"Initiate_Download for cid:%@",cid);

    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];

    NSString *fileName = [NSString stringWithFormat:@"%@.mp4",cid];
    NSString *destinationPath = [[self VideoDownloadFolderPath]stringByAppendingPathComponent:fileName];

    [request setDownloadDestinationPath:destinationPath];   
    [request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"%@-part",destinationPath]];
    [request setDelegate:self];

    NSDictionary *rqstDict = [NSDictionary dictionaryWithObjectsAndKeys:cid,@"cid",urlStr,@"url", nil];
    [request setUserInfo:rqstDict];
    [request setAllowResumeForFileDownloads:YES];
    [request startAsynchronous];
}

 //Delegate
- (void)requestStarted:(ASIHTTPRequest *)request1
{
    //some code
}
- (void)request:(ASIHTTPRequest *)request1 didReceiveResponseHeaders:(NSDictionary *)responseHeaders
{
   //some code
}
- (void)requestFinished:(ASIHTTPRequest *)request1
{
   //some code
}
- (void)requestFailed:(ASIHTTPRequest *)request1
{
  //some code
}
4

1 に答える 1

1

各リクエストのリクエストのURLと宛先パスを保存し、リクエストの使用コードを一時停止する必要があります:-

[キャンセルリクエスト];

リクエストを再開するには、同じURLと宛先パスで別のリクエストを作成する必要があります。例えば ​​:-

    ASIHTTPRequest *requestToResume = [ASIHTTPRequest requestWithURL:url];
    [requestToResume setTemporaryFileDownloadPath:tempfilePath];
    [requestToResume setDownloadDestinationPath:filePath]; 
    [requestToResume setDelegate:self];
    [requestToResume setDownloadProgressDelegate:self];
    [requestToResume setUserInfo:dictInfo];

    // This file has part of the download in it already
    [requestToResume setAllowResumeForFileDownloads:YES];
    [requestToResume setDidFinishSelector:@selector(requestDone:)];
    [requestToResume setDidFailSelector:@selector(requestWentWrong:)];
    [requestToResume startAsynchronous];

上記のコードでは、リクエストのuserInfoとして設定された辞書から曲のURLを取得し、リクエストを再開するためのこれらの詳細を取得します。リクエストを再開すると、一時停止した時点からファイルがダウンロードされるため、ファイルのダウンロードを再開する目的が解決されます。

于 2012-12-07T06:42:29.190 に答える