Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 549
Steve E Avatar asked Jul 17 '12 09:07

Steve E


2 Answers

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.

like image 173
CornPuff Avatar answered Nov 11 '22 23:11

CornPuff


The best solution for this would be to actually embed the stream url feed with two audio tracks! One would be with the normal audio and the other audio track would be the muted audio.

It makes more sense to do it this way rather then the way ComPuff suggested as his way your actually creating two separate URL streams - which is not required.

Here is the code that you could use to switch the audio tracks:

    float volume = 0.0f;

AVPlayerItem *currentItem = self.player.currentItem;
NSArray *audioTracks = self.player.currentItem.tracks;

DLog(@"%@",currentItem.tracks);

NSMutableArray *allAudioParams = [NSMutableArray array];

for (AVPlayerItemTrack *track in audioTracks)
{
    if ([track.assetTrack.mediaType isEqual:AVMediaTypeAudio])
    {
        AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];
        [audioInputParams setVolume:volume atTime:kCMTimeZero];
        [audioInputParams setTrackID:[track.assetTrack trackID]];
        [allAudioParams addObject:audioInputParams];
    }
}

if ([allAudioParams count] > 0) {
    AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
    [audioMix setInputParameters:allAudioParams];
    [currentItem setAudioMix:audioMix];
}

The only problem is that my stream url is only display two tracks (one for video and one for audio) when it should actually be three tracks (2 audio tracks). I cant work out if this is a problem with the stream url or my code! Can anyone spot any mistakes in the code?

like image 1
GameDev Avatar answered Nov 11 '22 21:11

GameDev