Siddharth
Siddharth

Reputation: 555

Getting an audio file size in kb

In my application I have an audio files stored in caf and aac format. I need to get the size of the audio file in kb,from the audio url. Is there any way to achieve this requirement?

Upvotes: 3

Views: 1705

Answers (4)

Siddharth
Siddharth

Reputation: 555

I solved it by converting it to NSData and getting its length property.

    NSError *err = nil;
    NSData *audioData = [NSData dataWithContentsOfFile:file options:0 error:&err];
    NSString *audioLength = [NSString stringWithFormat:@"%dkB",[audioData length]/1000];
    NSLog(@"[audioData bytes]==%@",audioLength);

Upvotes: 0

justin
justin

Reputation: 104698

You can use NSFileManager to request the attributes dictionary, then get the size from the attributes dictionary:

NSFileManager * fileManager = [NSFileManager new];
NSError * error = 0;
NSDictionary * attr = [fileManager attributesOfItemAtPath:path error:&error];
// error checking
unsigned long long size = [attr fileSize];
[fileManager release];

this is available on iOS 2 and OSX.5

Upvotes: 5

justin
justin

Reputation: 104698

You can use CF/NSURL's property accessors, which are available on iOS 5 and up, and OSX.6 and up:

id value = 0;
// Key for the file’s size in bytes, returned as an NSNumber object:
NSString * key = NSURLFileSizeKey;
NSError * error = 0;
BOOL result = [audioFileURL getResourceValue:&value forKey:key error:&error];
... error checking here ...
NSNumber * number = value;

There are multiple keys for file size information, another may be better suited for your needs.

Upvotes: 1

AbhishekKr
AbhishekKr

Reputation: 310

you could use following command-line

$ curl --head $URL_OF_AUDIO_FILE | grep Content-Length | awk '{print ($2/1024);}'

[[ Example for a PDF ]]

curl --head http://cran.r-project.org/doc/manuals/R-intro.pdf | grep Content-Length | awk '{print ($2/1024);}'

Upvotes: 1

Related Questions