iOS 获取音频时长的正确姿势 笔记

获取方式一

通过AVPlayerItemduration 来获取时长,总是差那么几秒,十几秒
CMTimeGetSeconds(self.player.currentItem.duration);


获取方式二

通过AVURLAssetduration 来获取时长

- (NSTimeInterval)audioDurationFromURL:(NSString *)url {
    AVURLAsset *audioAsset = nil;
    NSDictionary *dic = @{AVURLAssetPreferPreciseDurationAndTimingKey:@(YES)};
    if ([url hasPrefix:@"http://"]) {
        audioAsset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:url] options:dic];
    }else {
        audioAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:url] options:dic];
    }
    CMTime audioDuration = audioAsset.duration;
    float audioDurationSeconds = CMTimeGetSeconds(audioDuration);
    return audioDurationSeconds;
}

如果还是不对,请移步

参考

https://www.jianshu.com/p/a3c171378486


有趣的点赞动画

https://github.com/xjh093/JHLikeButton


你可能感兴趣的:(iOS)