AVPlayer Volume Control

后端 未结 5 777
醉梦人生
醉梦人生 2021-01-18 03:57

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 v

5条回答
  •  伪装坚强ぢ
    2021-01-18 04:45

    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
     }
    

提交回复
热议问题