AVPlayer动态音量控制 [英] AVPlayer Dynamic Volume control
问题描述
如何动态更改AVPlayer的音量?我的意思是,我想在每次按下按钮时使音量静音.给定的代码似乎仅在编译时就对其进行了更改.运行期间如何做???
How can I change the volume of the AVPlayer Dynamically? I mean, I want to mute the volume every time a button is pressed. the given code seems to change it in compile time only. How to do it during runtime???
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];
推荐答案
您可以在播放期间发送playerItem
AVMutableAudioMix的新实例,以动态更改级别.只需将您的按钮链接到一个使用适当值创建一个新AVMutableAudioMix实例的操作方法(如您在上面所做的),然后使用playerItem的setAudioMix:
方法来设置新的混合值. (如果您正在使用方法,请不要忘记保存对playerItem实例的引用以供以后访问.)
You can send playerItem
new instances of AVMutableAudioMix during playback to change levels dynamically. Just link your button to an action method that creates a new AVMutableAudioMix instance (like you have done above) with the appropriate values, and use playerItem's setAudioMix:
method to set the new mix values. (If you're working across methods, don't forget to save a reference to your playerItem instance to access it later.)
(NB setAudioMix:
在 AVPlayerItem文档,因为它是audioMix
属性的综合设置器.)
(N.B. setAudioMix:
isn't mentioned explicitly in the AVPlayerItem docs because it is a synthesized setter for the audioMix
property.)
这篇关于AVPlayer动态音量控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!