AVPlayer Volume Control

后端 未结 5 782
醉梦人生
醉梦人生 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:51

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

提交回复
热议问题