我想创建一个按钮来静音来自AVPlayer的音频。
为什么我不能将.volume与AVPlayer一起使用,只能使用AVAudioPlayer? 还有另一种改变音量的方法吗?
例如music.volume = 0.0;
感谢您的回答。
答案 0 :(得分:13)
从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
}
答案 1 :(得分:3)
我使用下面的代码来静音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];
答案 2 :(得分:2)
最佳方式如下
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];
答案 3 :(得分:0)
查看此链接
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];
答案 4 :(得分:0)
从iOS 7开始,muted
上还有一个AVPlayer
属性:
@property (nonatomic, getter=isMuted) BOOL muted NS_AVAILABLE(10_7, 7_0);