我正在尝试基于AVPlayer AVAudioMix AVAudioMixInputParameters实现淡入效果 . 它基本上有效,除了在启动我的应用程序后第一次播放音频时,开头有一个点击 . 虽然后续的播放效果很好,但是第一次出现故障非常稳定且可重复 .

我的播放按钮仅在AVPlayerItem的 status 设置为就绪后启用,因此它在加载音频文件和构建所有对象后等待多长时间 .

这发生在OS X上,我还没有在iOS上测试它(尚未) .

请注意,对于此测试,您需要一个以声音而非静音开头的音频文件 . 这是我没有GUI部分的精简代码( testFadeIn 是入口点):

static AVPlayer* player;
static void* PlayerItemStatusObserverContext = &PlayerItemStatusObserverContext;

- (void)testFadeIn
{
    AVURLAsset* asset = [AVURLAsset.alloc initWithURL:[NSURL fileURLWithPath:@"Helicopter.m4a"] options:@{AVURLAssetPreferPreciseDurationAndTimingKey: @YES}];
    AVPlayerItem* item = [AVPlayerItem playerItemWithAsset:asset];
    player = [AVPlayer playerWithPlayerItem:item];
    [item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:PlayerItemStatusObserverContext];
}

- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
    if (context == PlayerItemStatusObserverContext)
    {
        AVPlayerStatus status = (AVPlayerStatus)[[change objectForKey:NSKeyValueChangeNewKey] integerValue];
        if (status == AVPlayerStatusReadyToPlay)
        {
            [self applyFadeIn];
            [self performSelector:@selector(play:) withObject:nil afterDelay:1.0];
        }
    }
}

- (void)applyFadeIn
{
    assert(player.currentItem.tracks.firstObject);
    AVMutableAudioMixInputParameters* fadeIn = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:player.currentItem.tracks.firstObject];
    [fadeIn setVolume:0 atTime:kCMTimeZero];
    [fadeIn setVolume:1 atTime:CMTimeMake(2, 1)];
    NSMutableArray* paramsArray = [NSMutableArray new];
    [paramsArray addObject:fadeIn];
    AVMutableAudioMix* audioMix = [AVMutableAudioMix audioMix];
    audioMix.inputParameters = paramsArray;
    player.currentItem.audioMix = audioMix;
}

- (void)play:(id)unused
{
    [player play];
}

点击!这有什么问题?

Edit

我现在使用的一个明显的解决方法是:当播放器报告它已准备就绪时,我会以音量= 0进行短暂的100ms播放,然后恢复当前时间和音量,然后我向主应用程序报告播放器已准备就绪 . 这种方式没有点击 . 有趣的是,任何小于100毫秒的东西仍然会给出点击 .

这似乎是第一次播放后由AVFoundation缓存的问题 . 这既不是轨道,因为当我设置了参数的淡入淡出时它们是可用的,也不是寻找状态 .