AVPlayer动态音量控制

时间:2012-04-18 09:54:57

标签: objective-c ios media-player avfoundation avplayer

如何动态更改AVPlayer的音量?我的意思是,每次按下按钮时我都要将音量静音。给定的代码似乎只在编译时改变它。如何在运行时间做到这一点???

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[self myAssetURL] options:nil];
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
    AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters];
    [audioInputParams setVolume:0.0 atTime:kCMTimeZero];
    [audioInputParams setTrackID:[track trackID]];
    [allAudioParams addObject:audioInputParams];
}
AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix];
[audioZeroMix setInputParameters:allAudioParams];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem setAudioMix:audioZeroMix]; 
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
self.mPlayer = player;
[mPlayer play];

1 个答案:

答案 0 :(得分:2)

您可以在播放期间发送playerItem个新的AVMutableAudioMix实例,以动态更改关卡。只需将您的按钮链接到一个动作方法,该方法使用适当的值创建一个新的AVMutableAudioMix实例(就像您上面所做的那样),并使用playerItem的setAudioMix:方法设置新的混合值。 (如果您正在使用方法,请不要忘记保存对playerItem实例的引用以便以后访问它。)

AVPlayerItem docs中未明确提及{N.B。setAudioMix:,因为它是audioMix属性的合成设置器。)