3

如何动态更改 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];
4

1 回答 1

2

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

(NBsetAudioMix:AVPlayerItem 文档中没有明确提到,因为它是audioMix属性的综合设置器。)

于 2012-04-18T21:05:57.533 回答