无法在iOS上从Web加载MP3文件

时间:2014-06-30 10:09:14

标签: ios asihttprequest

网址是" http://pics2.xiaoma.com/xiaoma/bbs/app/tpo/ky/TPO27-3.mp3"。 此URL是MP3文件,我无法正常播放。这是我正在使用的代码。需要指导我在这里做错了什么。

+ (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;  }

日志几乎是" 0000000 00000000 ..."。

正确的响应可以正常播放的节目是

{
"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";}

无法播放的错误responseHeaders是

{
"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";}

感谢。

1 个答案:

答案 0 :(得分:0)

根本不确定这是否是你的问题,但MP3花了很长时间才开始下载(即使它只有2.1mb。)

[request setTimeOutSeconds:20];

下载开始之前是否有可能超时?

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