I have an mp3 file on a server. I want to get this file's information like what's the size of this file, what's the artists name, what's the album name, when was the file created, when was it modified, etc. I want all this information.
Is it possible to get this information without actually downloading the whole file? Using NSURLConnection
or otherwise?
EDIT:
The following code doesn't give me the required information, i.e. file created by, artist name, etc
NSError *rerror = nil;
NSURLResponse *response = nil;
NSURL *url = [NSURL URLWithString:@"http://link.to.mp3"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"HEAD"];
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&rerror];
NSString *resultString = [[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"URL: %@", url);
NSLog(@"Request: %@", request);
NSLog(@"Result (NSData): %@", result);
NSLog(@"Result (NSString): %@", resultString);
NSLog(@"Response: %@", response);
NSLog(@"Error: %@", rerror);
if ([response isMemberOfClass:[NSHTTPURLResponse class]]) {
NSLog(@"AllHeaderFields: %@", [((NSHTTPURLResponse *)response) allHeaderFields]);
}
The "AllHeaderFields" is:
AllHeaderFields: {
"Cache-Control" = "max-age=0";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/plain; charset=ascii";
Date = "Fri, 17 Feb 2012 12:44:59 GMT";
Etag = 19202n;
Pragma = public;
Server = dbws;
"x-robots-tag" = "noindex,nofollow";
}
It is quite possible to get the ID3 information embedded in an MP3 file (artist name, track title) without downloading the whole file or using low-level APIs. The functionality is part of the
AVFoundation
framework.The class to look at is
AVAsset
and specifically it's network friendly subclassAVURLAsset
.AVAsset
has anNSArray
property namedcommonMetadata
. ThiscommonMetadata
property will contain instances ofAVMetadataItem
, assuming of course that the reference URL contains metadata. You will usually use theAVMetadataItem
'scommonKey
property to reference the item. I find this method of iterating through an array checkingcommonKeys
irritating so I create anNSDictionary
using thecommonKey
property as thekey
and thevalue
property as theobject
. Like so:With the addition of this simple method the
AVAsset
's metadata becomes quite easy to use. Here is an example of getting an MP3's metadata through a URL:I have found that this code seems to load just the first few seconds of your MP3 file. Also note that this code is synchronous; So use with caution. But
AVURLAsset
does have some async functionality described in the docs.Once you have the
AVAsset
you can create aAVPlayerItem
with it and feed that to anAVPlayer
and play it, or not.Yes and no. Things like the file size and modification date often come as part of the HEAD response. But not always: with a lot of dynamic URLs, you won't get all of the information.
As for the artist and album name, they're part of the MP3's ID3, which is contained inside the file, and so you won't be able to get them with a HEAD request. Since the ID3 tag is typically at the beginning of a file, you could try to grab just that part and then read the ID3 tag. But you won't be able to do it with
NSURLConnection
since it doesn't support just fetching part of a file, so you'll need to find a more low-level way of getting data by HTTP.Yep, you're right on target with NSURLConnection.
I think you want to send a HEAD request for the resource you want information about and then check the information you receive in
connection:didReceiveResponse:
andconnection:didReceiveData:
Edit
Admittedly I didn't read your question in its entirety. It won't be possible to get ID3 information, but you should be able to get size of file and maybe creation date etc.
This answer does give some good information about how to get the ID3 information. You'd need to set up a php page to examine the mp3 file server-side and return just that information you require instead of the entire mp3.