AVPlayer Volume Control

若如初见. 提交于 2019-12-19 18:49:32

问题


I want to create a button that mutes the audio from an AVPlayer.

Why can´t I use .volume with AVPlayer, only with AVAudioPlayer? Is there another way to change the volume?

e.g music.volume = 0.0;

Thanks for your answers.


回答1:


Starting in iOS 7, simply call:

myAvPlayer.volume = 0;

Otherwise, you'll have to use the annoying setAudioMix solution. I'm detecting support for this in my app as follows:

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
 }



回答2:


I used below code to mute 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];



回答3:


best way is as following

  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];



回答4:


Check out this link

apple documentation for muting audio

Below is the code

 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:


As of iOS 7 there is also a muted property on AVPlayer:

@property (nonatomic, getter=isMuted) BOOL muted NS_AVAILABLE(10_7, 7_0);


来源:https://stackoverflow.com/questions/15569983/avplayer-volume-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!