13

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.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Rameswar Prasad
  • 1,331
  • 17
  • 35

6 Answers6

23

Probably you have used

  NSURL *itemPathURL = [NSURL URLWithString:outPath];

instead of

NSURL *itemPathURL = [NSURL fileURLWithPath:outPath];
Mr.G
  • 1,275
  • 1
  • 18
  • 48
2

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)
Cale
  • 361
  • 3
  • 10
  • I came across a similar problem when opening a local file. My solution was to include a asset.init() after 'asset = AVURLAsset(...)' The duration was then populated with the correct length of the movie. I'm writing in C# so there might be differences. But I landed on this topic while trying to find a solution. – Morten Apr 07 '20 at 11:32
1

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)
Hiren Panchal
  • 2,963
  • 1
  • 25
  • 21
1

for my case, the filename's extension have to be recognized, like ".mp3". if not, it will return 0.

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

Haiyuan
  • 103
  • 2
  • 5
0

Try like 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);
Bhoomi Jagani
  • 2,413
  • 18
  • 24
  • Yes the same thing I am trying the first part of your code is in another method in my case and there I am getting the URL just like you did, so that's not the prblem. – Rameswar Prasad Sep 26 '14 at 11:02
-3

Try this....

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);
Hiren Varu
  • 1,447
  • 1
  • 9
  • 8
  • 1
    Did you even read the question? He said that the duration was 0. Formatting it into minutes and seconds makes absolutely no difference at all. – Jarrod Robins May 20 '15 at 02:08