assetURL = [item valueForProperty: MPMediaItemPropertyAssetURL];
NSLog(@"%@", assetURL); // asset url declares the song url and song path
AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:assetURL options:nil]; // declare the audio asset url
CMTime audioDuration = audioAsset.duration; // get duration in cm time
double audioDurationSeconds = CMTimeGetSeconds(audioDuration); // convert audio duration in double
但这是一个问题,因为我只在几秒钟内获得音频持续时间,但我需要歌曲持续时间的“MM:SS”格式。
请帮我。提前致谢。
NSUInteger audioDurationSeconds = (long)CMTimeGetSeconds(audioDuration);
NSUInteger minutes = floor(audioDurationSeconds % 3600 / 60);
NSUInteger seconds = floor(audioDurationSeconds % 3600 % 60);
NSString *time = [NSString stringWithFormat:@"%02ld:%02ld", minutes, seconds];