Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVURLAsset returns duration as 0

I am trying to get the duration of the audio files for trimming them, I am using the below code,

audioAsset = [AVURLAsset assetWithURL:itemURL];

CMTime assetTime = [audioAsset duration];
Float64 duration = CMTimeGetSeconds(assetTime);

when I provide itemURL of any audio file of media library, I get the proper duration, and then I am able to trim the audio file. Then I save the trimmed audio file in document directory. But when I try to trim the already trimmed file the same code returns me 0 as duration. However I am able to play the trimmed file and get the duration using AVAudioPlayer, but what is the problem with AVURLAsset I am not able to figure out.

Can anyone help? I have already tried almost all the answers of such question in stackoverflow.

like image 373
Rameswar Prasad Avatar asked Sep 26 '14 06:09

Rameswar Prasad


1 Answers

I think AVURLAsset needs to know the type extension of your file to compute its duration. You're probably storing your file as {fileName} and not as {fileName}.{extension}.

If you run the following code with yourFileName without extension you get *** Could not find format reader for URL, and return value 0.

import AVFoundation

let fileName = *yourFileName*
let url = NSURL(fileURLWithPath: fileName)
let asset = AVURLAsset(URL: url, options: nil)
println(asset.duration.value.description)
like image 172
Cale Avatar answered Oct 21 '22 04:10

Cale