AVURLAsset returns duration as 0

I am trying to get the duration of audio files to trim them, I use the code below,

audioAsset = [AVURLAsset assetWithURL:itemURL]; CMTime assetTime = [audioAsset duration]; Float64 duration = CMTimeGetSeconds(assetTime); 

when I provide the itemURL of any audio file of a media library, I get the proper duration, and then I can trim the audio file. Then I save the trimmed audio file in the document directory. But when I try to trim an already trimmed file, the same code returns me 0 as the duration. However, I can play the cropped file and get the duration using AVAudioPlayer, but what is the problem with AVURLAsset that I cannot understand.

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

+7
ios audio duration avurlasset
source share
6 answers

You may have used

  NSURL *itemPathURL = [NSURL URLWithString:outPath]; 

instead

 NSURL *itemPathURL = [NSURL fileURLWithPath:outPath]; 
+10
source share

For Swift 3.0 and above

import avfoundation

  let asset = AVAsset(url: URL(fileURLWithPath: ("Your File Path here")!)) let totalSeconds = Int(CMTimeGetSeconds(asset.duration)) let minutes = totalSeconds / 60 let seconds = totalSeconds % 60 let mediaDuration = String(format:"%02i:%02i",minutes, seconds) 
+1
source share

Try the following .....

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *getAudioPath = [documentsDirectory stringByAppendingPathComponent:@"song.mp3"]; NSURL *finalUrl=[[NSURL alloc]initFileURLWithPath:getAudioPath]; NSLog(@"finalUrl===%@",finalUrl); AVURLAsset *asset = [AVURLAsset assetWithURL: finalUrl]; duration = CMTimeGetSeconds(asset.duration); NSLog(@"duration==%f",duration); 
0
source share

I think that AVURLAsset should know the type extension of your file in order to calculate its duration. You are probably saving your file as {fileName} and not as {fileName}.{extension} .

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

 import AVFoundation let fileName = *yourFileName* let url = NSURL(fileURLWithPath: fileName) let asset = AVURLAsset(URL: url, options: nil) println(asset.duration.value.description) 
0
source share

for my case, the file name extension should be recognized, for example ".mp3". if not, it will return 0.

... let asset = AVURLAsset (url: URL (fileURLWithPath: filefullname), options: nil) ... asset.duration ...

0
source share

Try it....

 AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:Yoururl options:nil]; CMTime time = asset.duration; double durationInSeconds = CMTimeGetSeconds(time); int minutes = floor(durationInSeconds/60); int seconds = round(durationInSeconds - (minutes * 60)); NSString * duration = [NSString stringWithFormat:@"%i:%i",minutes,seconds]; NSLog(@"Duration : %@", duration); 
-one
source share

All Articles