AVAudioPlayer.duration不等于AVURLAsset.duration

时间:2018-05-31 09:16:57

标签: avaudioplayer avurlasset

我按AVAudioEngin录制音频文件,格式为:

@{ AVSampleRateKey:@(44100.0), AVNumberOfChannelsKey: @(1), AVFormatIDKey: @(kAudioFormatMPEG4AAC), }

然后我用AVAudioPlayer

播放它
self.player = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:self.outFilePath] error:nil];
NSTimeInterval duration = _player.duration; // 409.73357109834228

AVURLAsset*audioAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:self.outFilePath] options:nil];
CMTime audioDuration = audioAsset.duration;
Float64 seconds = CMTimeGetSeconds(audioDuration); // 429.31374149659865

为什么duration不等于seconds

我是否错过了为AVAudioPlayer设置的某些属性?

1 个答案:

答案 0 :(得分:0)

我在AVAsset.h中看到了一些关于持续时间属性的选项。如下: Indicates the duration of the asset. If @"providesPreciseDurationAndTiming" is NO, a best-available estimate of the duration is returned. The degree of precision preferred for timing-related properties can be set at initialization time for assets initialized with URLs. See AVURLAssetPreferPreciseDurationAndTimingKey for AVURLAsset below. 它告诉我,当提供PreiseDurationAndTiming 为NO时,持续时间不完全正确,你应该将 providePreciseDurationAndTiming 设置为YES,只需试一试。