Search code examples
objective-ciosstreamingavplayerhttp-live-streaming

Mute an HTTP Live Stream in an AVPlayer


I've been trying to work out this problem for a good 48 hours now and haven't come up with anything. I have 2 AVPlayer objects playing different http live streams. Obviously, I don't want them both playing audio at the same time so I need a way to mute one of the videos.

Apple suggests this for muting an audio track playing in AVPlayer...

NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVPlayerItemTrack *track in [_playerItem tracks]) {
    if ([track.assetTrack.mediaType isEqualToString:AVMediaTypeAudio]) {
        AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];
        [audioInputParams setVolume:0.0 atTime:CMTimeMakeWithSeconds(0,1)];
        [audioInputParams setTrackID:[track.assetTrack trackID]];
        [allAudioParams addObject:audioInputParams];
        // Added to what Apple Suggested
        [track setEnabled:NO];
    }
}

AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix];
[audioZeroMix setInputParameters:allAudioParams];
[_playerItem setAudioMix:audioZeroMix];

When this didn't work (after many iterations), I found the enabled property of AVPlayerItemTrack and tried setting that to NO. Also nothing. This doesn't even register as doing anything because when I try an NSLog(@"%x",track.enabled), it still shows up as 1.

I'm at a loss and I can't think of another piece of documentation I can read and re-read to get a good answer. If anyone out there can help, that would be fantastic.

*Update: I got a hold of Apple and according to the AVFoundation team, it is impossible to mute or disable a track of an HLS video. I, personally, feel like this is a bug so I submitted a bug report (You should do the same to tell Apple that this is a problem). You can also try and submit a feature enhancement request via their feedback page.


Solution

  • New iOS 7 answer: AVPlayer now has 2 new properties 'volume' and 'muted'. Use those!


    And here is the original answer for life before iOS 7:

    I've been dealing with the same thing. We created muted streams and streams with audio. To mute or unmute you call [player replaceCurrentItemWithPlayerItem:muteStream].

    I also submitted a bug report. It looks like AVPlayer has this functionality on MacOS 10.7, but it hasn't made it to iOS yet.

    AVAudioMix is documented not to work on URL assets here

    Of course I tried it anyway, and like you I found it really doesn't work.