简体   繁体   中英

How to mute video while playing an mp3 with AVAudioPlayer

I have a video with audio, and an mp3. I would like to play the video in mute (audio off) while playing an mp3. I am using MPMoviePlayerController to play the video, and AVAudioPlayer to play the mp3. Is there a way to do it?

MPMoviePlayerController does not appear to give you any control over volume. I would suggest you have a look at AVPlayer and the accompanying examples. It's a bit more work to play video, but you can use the "muted" property to do exactly what you want.

You can actually use the MPMusicPlayerController for achieving what you aim for.

- (void)muteVidepPlayer:(BOOL)muteFlag
{
    MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
    if (muteFlag)
    {
        self.lastVolume = musicPlayer.volume; 
        [musicPlayer setVolume:0.0f];
    }
    else
    {
        [musicPlayer setVolume:self.lastVolume];
    }
}

What it does, it uses an instance of MPMusicPlayerController which allows you to adjust the volume. Do not get too puzzled, this will effectively adjust the volume of your MPMoviePlayerController instance as well. This works because MPMoviePlayerController and MPMusicPlayerController both use the app audio session (by default).

For making sure that you are able to reset the volume to the former setup, this method tries to store that value in an instance variable of type float ( self.lastVolume ) of your playback controller / viewcontroller / whatever.

I chose to answer my own question: @nickbona answer was fine but it has no simple and example code. So the solution is to use AVPlayer instead of MPMoviePlayerController.

So here I am with an easy source code example if someone else will need it:

//init player
AVAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"yourfile" ofType:@"mp4"]] options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer *myPlayerController = [AVPlayer playerWithPlayerItem:anItem];

//disable audio (this is the version when you have more than one video in the playlist: i write this version so it should be more useful)
NSArray *playerTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *playerParams = [NSMutableArray array];
for (AVAssetTrack *track in playerTracks) {
    AVMutableAudioMixInputParameters *audioInputParams =    [AVMutableAudioMixInputParameters audioMixInputParameters];
    [audioInputParams setVolume:0.0 atTime:kCMTimeZero];
    [audioInputParams setTrackID:[track trackID]];
    [playerParams addObject:audioInputParams];
}
AVMutableAudioMix *muteAudioMix = [AVMutableAudioMix audioMix];
[muteAudioMix setInputParameters:playerParams];
[[myPlayerController currentItem] setAudioMix:muteAudioMix];

Hope it can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM