1

我正在尝试找到一种方法来淡入本地保存的音轨而不会出现音频故障。我正在使用 AVPlayer 并使用 AVURLAsset 从 iPodLibrary 引用 mp3。以下方法大部分时间都有效,但并非一直有效,所以我想我需要扫描音频数据以寻找最近的音量过零并从那里淡入淡出。任何指针将不胜感激。

float duration = 0.5;
AVAsset *asset = [self.av_Player.currentItem asset];
NSArray *keys = [NSArray arrayWithObject:@"tracks"];

[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^(void) {
    NSError *error = nil;

    NSTimeInterval now = [self currentPlaybackTime];
    CMTime mainFadeIn = CMTimeMakeWithSeconds(now, 6000);
    CMTime mainFadeDuration = CMTimeMakeWithSeconds(duration, 6000);
    CMTimeRange timerange = CMTimeRangeMake(mainFadeIn, mainFadeDuration);

    AVKeyValueStatus trackStatus = [asset statusOfValueForKey:@"tracks" error:&error];

    switch (trackStatus) {
        case AVKeyValueStatusLoaded:
            if( self.av_Player )    {
                NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeAudio];

                AVMutableAudioMixInputParameters *volumeMixInput = [AVMutableAudioMixInputParameters audioMixInputParameters];
                [volumeMixInput setVolumeRampFromStartVolume:0.0 toEndVolume:tovolume timeRange:timerange];
                [volumeMixInput setTrackID:[[tracks objectAtIndex:0] trackID]];

                AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
                [audioMix setInputParameters:[NSArray arrayWithObject:volumeMixInput]];
                [self.av_Player.currentItem setAudioMix:audioMix];

            }
            break;
        default:
            break;
    }
}];
4

1 回答 1

0

不知道为什么我使用上述方法会出现故障,但假设它可能与使用块有关。无论如何,我使用了下面的代码,它现在可以顺利淡入和淡出 - 希望这对其他人有所帮助。

NSTimeInterval now = [self currentPlaybackTime];

AVPlayerItem *playerItem = self.av_Player.currentItem;

AVAsset *asset = playerItem.asset;
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];

NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
    AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];
    [audioInputParams setVolume:fromvolume atTime:CMTimeMakeWithSeconds(now-0.1, 6000)];
    [audioInputParams setVolume:fromvolume atTime:CMTimeMakeWithSeconds(now, 6000)];
    [audioInputParams setVolume:tovolume atTime:CMTimeMakeWithSeconds(now+duration, 6000)];
    [audioInputParams setTrackID:[track trackID]];
    [allAudioParams addObject:audioInputParams];
}

AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
[audioMix setInputParameters:allAudioParams];

[playerItem setAudioMix:audioMix];
于 2013-01-24T12:06:53.900 回答