I need to play remote mp3 files in my app. I'm using AVURLAsset
and AVPlayer
to do it. Everything is fine, but some files has incorrect length. I need correct length to display progress bar of track playback.
Here is mp3 file with wrong length: http://content.mts.stream.ru/files/melody/mp3/3828.mp3
Here is how I try to get track length:
AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
Float64 assetDuration = CMTimeGetSeconds(asset.duration);
Float64 itemDuration = CMTimeGetSeconds(item.duration);
Both assetDuration
and itemDuration
is ~45 seconds. Real track length is about 30 seconds.
How can I get real length of mp3 file? Looks like there is an error in mp3 tags, because preview utility on my Mac also says that it is 45 seconds. However iTunes says it's 30 seconds.
I have an idea that I can get content-length field of HTTP response and calculate track size, but I need to get bitrate.
UPDATE
I've went deeper and tried to use AudioToolbox:
AudioFileID fileID;
OSStatus result = AudioFileOpenURL((__bridge CFURLRef)url, kAudioFileReadPermission, 0, &fileID);
UInt32 bitrate = 0;
UInt32 bitrateSize = sizeof(UInt32);
result = AudioFileGetProperty(fileID, kAudioFilePropertyBitRate, &bitrateSize, &bitrate);
UInt64 size = 0;
UInt32 sizeSize = sizeof(UInt64);
result = AudioFileGetProperty(fileID, kAudioFilePropertyAudioDataByteCount, &sizeSize, &size);
AudioFileClose(fileID);
UInt64 seconds = size / (bitrate / 8);
And got wrong value again. So it looks like this files has some problems with encoding. I'll update this question if I'll get further results.