2
assetURL = [item valueForProperty: MPMediaItemPropertyAssetURL];
NSLog(@"%@", assetURL); // get song url
AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil]; // add song url into AVURLasset
CMTime audioDuration = audioAsset.duration; //get duration in second
int  audioDurationSeconds = CMTimeGetSeconds(audioDuration); // get song duration in seconds.....

in this code song duration get into second i need song duration into minutes how it is possible ... thanx

3 Answers3

2
 -(NSString *)trackDuration:(NSInteger)duration {
     NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
     return  [formatter stringFromTimeInterval:duration];
 }

This is the least mathematical route.

For the SpotifySDK I had to divide duration by 1000. Go figure.

John Franke
  • 1,444
  • 19
  • 23
1
CMTime audioDuration = audioAsset.duration;
NSUInteger totalSeconds = CMTimeGetSeconds(audioDuration);
NSUInteger hours = floor(totalSeconds / 3600);
NSUInteger minutes = floor(totalSeconds % 3600 / 60);
NSUInteger seconds = floor(totalSeconds % 3600 % 60);

NSLog(@"hours = %i, minutes = %02i, seconds = %02i",hours,minutes,seconds);
nilam_mande
  • 931
  • 7
  • 14
-1

Convert seconds(int) to minute(double).

Keyur Akbari
  • 182
  • 1
  • 13