0

我录制了一个音频文件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吗?

4

1 回答 1

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. 它告诉当providesPreciseDurationAndTiming为NO时,持续时间不完全正确,你应该将providesPreciseDurationAndTiming设置为YES,试一试。

于 2018-05-31T09:40:51.710 回答