AVPlayer 音量控制
Posted
技术标签:
【中文标题】AVPlayer 音量控制【英文标题】:AVPlayer Volume Control 【发布时间】:2013-03-22 12:10:55 【问题描述】:我想创建一个按钮,将来自 AVPlayer 的音频静音。
为什么我不能将 .volume 与 AVPlayer 一起使用,而只能与 AVAudioPlayer 一起使用? 还有其他方法可以改变音量吗?
例如music.volume = 0.0;
感谢您的回答。
【问题讨论】:
【参考方案1】:从 ios 7 开始,只需调用:
myAvPlayer.volume = 0;
否则,您将不得不使用烦人的setAudioMix
解决方案。我在我的应用中检测到对此的支持如下:
if ([mPlayer respondsToSelector:@selector(setVolume:)])
mPlayer.volume = 0.0;
else
NSArray *audioTracks = mPlayerItem.asset.tracks;
// Mute all the audio tracks
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];
[mPlayerItem setAudioMix:audioZeroMix]; // Mute the player item
【讨论】:
谢谢道格!我找了几个小时。值得庆幸的是,它只是在 AVPLayer 上设置了一个属性:D 完美、简单、漂亮的答案! @doug 你为什么把它改成1.0
- 只是一个错字?
@CupawnTae 哇,这是一个很好的问题。是的,我真的很困,做了一个虚假的修正,哈哈。我改回来了。【参考方案2】:
我使用下面的代码来静音 AVPlayer。
float volSet = 0 ;
AVAsset *avAsset = [[avPlayer currentItem] asset] ;
NSArray *audioTracks = [avAsset tracksWithMediaType:AVMediaTypeAudio] ;
NSMutableArray *allAudioParams = [NSMutableArray array] ;
for(AVAssetTrack *track in audioTracks)
AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters] ;
[audioInputParams setVolume:volSet atTime:kCMTimeZero] ;
[audioInputParams setTrackID:[track trackID]] ;
[allAudioParams addObject:audioInputParams];
AVMutableAudioMix *audioVolMix = [AVMutableAudioMix audioMix] ;
[audioVolMix setInputParameters:allAudioParams];
[[avPlayer currentItem] setAudioMix:audioVolMix];
【讨论】:
这有效:[[MPMusicPlayerController applicationMusicPlayer] setVolume:0.0];【参考方案3】:最好的方法如下
AVAssetTrack* assetTrackAudio = [arrayTracks objectAtIndex:0];
AVMutableAudioMixInputParameters* audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];
[audioInputParams setVolume:currentVolume atTime:playerCursorStartPosition];
[audioInputParams setTrackID:[assetTrackAudio trackID]];
NSArray* audioParams = [NSArray arrayWithObject:audioInputParams];
AVMutableAudioMix* audioMix = [AVMutableAudioMix audioMix];
[audioMix setInputParameters:audioParams];
AVPlayerItem* item = [player currentItem];
[item setAudioMix:audioMix];
【讨论】:
我认为它只适用于 AVAudioPlayer。当我将它与 AVPlayer 一起使用时,什么也没有发生。 @user2196602:它肯定适用于 AVPlayer。请查看apple documentation【参考方案4】:查看此链接
apple documentation for muting audio
下面是代码
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[self myAssetURL] options:nil];
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
// Mute all the audio tracks
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];
// Create a player item
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem setAudioMix:audioZeroMix]; // Mute the player item
// Create a new Player, and set the player to use the player item
// with the muted audio mix
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
// assign player object to an instance variable
self.mPlayer = player;
// play the muted audio
[mPlayer play];
【讨论】:
【参考方案5】:在 iOS 7 中,AVPlayer
上还有一个 muted
属性:
@property (nonatomic, getter=isMuted) BOOL muted NS_AVAILABLE(10_7, 7_0);
【讨论】:
以上是关于AVPlayer 音量控制的主要内容,如果未能解决你的问题,请参考以下文章