-1

The url is "http://pics2.xiaoma.com/xiaoma/bbs/app/tpo/ky/TPO27-3.mp3". This URL is a MP3 file and I am unable to play this normally. This is the code that I am using. Need guidance on what am I doing wrong here.

+ (ASIHTTPRequest *)requestAudioDataWithURLString:(NSString *)urlString
                                   completion:(CompeletionHandle)block {

//3.创建请求
ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setTimeOutSeconds:20];
[request setRequestMethod:@"GET"];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setDownloadCache:[ASIDownloadCache sharedCache]];

//4.开始异步请求
[request startAsynchronous];

//5.请求成功
__weak ASIFormDataRequest *weakRequest = request;
[request setCompletionBlock:^{
    //
    ASIFormDataRequest *strongRequest = weakRequest;
    NSData *data = strongRequest.responseData;
    if (weakRequest.didUseCachedResponse) {
        MYLog(@"使用缓存数据");
    }
    else
    {
        MYLog(@"请求网络数据");
    }
    block(data);
}];

//6.请求失败
[request setFailedBlock:^{
    ASIFormDataRequest *strongRequest = weakRequest;
    block(strongRequest.error);
    MYLog(@"%@",strongRequest.error);
}];

return request;  }

The log is almost "0000000 00000000 ...".

The right responseHeaders which can play normally are that

{
"Accept-Ranges" = bytes;
Age = 2;
"Cache-Control" = "max-age=633841";
Connection = close;
"Content-Length" = 1659957;
"Content-Type" = "audio/mpeg";
Date = "Mon, 30 Jun 2014 10:05:29 GMT";
Expires = "Mon, 07 Jul 2014 18:09:30 GMT";
"Last-Modified" = "Thu, 05 Jun 2014 03:05:47 GMT";
Server = "marco/0.1";
"X-Cache" = "MISS|MISS from cache_img_94, MISS from ctn-zj-ngb-107, MISS from usn-us-sms-058";
"X-Source" = "U/200";}

The error responseHeaders which can not play are that

{
"Accept-Ranges" = bytes;
Age = 0;
"Cache-Control" = "max-age=622695";
Connection = close;
"Content-Length" = 3028113;
"Content-Type" = "audio/mpeg";
Date = "Mon, 30 Jun 2014 09:46:16 GMT";
Expires = "Thu, 03 Jul 2014 19:39:02 GMT";
"Last-Modified" = "Thu, 05 Jun 2014 03:06:23 GMT";
Server = "marco/0.1";
"X-Cache" = "MISS from usn-us-sms-058";
"X-Source" = "U/304";}

{
"Accept-Ranges" = bytes;
Age = 280748;
"Cache-Control" = "max-age=639955";
Connection = close;
"Content-Length" = 2157922;
"Content-Type" = "audio/mpeg";
Date = "Mon, 30 Jun 2014 09:28:04 GMT";
Expires = "Fri, 04 Jul 2014 13:14:53 GMT";
"Last-Modified" = "Thu, 05 Jun 2014 03:06:59 GMT";
Server = "marco/0.1";
"X-ASIHTTPRequest-Expires" = "1404760415.688892";
"X-ASIHTTPRequest-Response-Status-Code" = 200;
"X-Cache" = "MISS|MISS from cache_img_88, MISS from ctn-zj-ngb-099, HIT from usn-us-sms-058";
"X-Source" = "U/200";}

Thanks.

zdy
  • 1
  • 4

1 Answers1

0

Not at all sure if this is your issue, but the MP3 took a very long time for me to start to download (even though it is only 2.1mb.)

[request setTimeOutSeconds:20];

Is it possible it is timing out before the download starts?

https://github.com/pokeb/asi-http-request/issues/161

nycynik
  • 7,371
  • 8
  • 62
  • 87