AVPlayer volume control

The only way to get volume changes in the current playback of AVPlayer elements is to follow this process;

  • Unload the currently playing AVPlayerItem resource.
  • Capture the current play time for this AVPlayerItem
  • Upload asset to new AVPlayerItem
  • Replace current AVPlayerItem with new
  • Wait until currentItem changes to AVPlayer
  • Prepare AudioMix and find the previous playback time

Am I missing some basic principle somewhere or does it mean that it was messed up to just control the volume levels?

I cannot use AVAudioPlayer because I need to download iTunes tracks to the player.

+18
objective-c iphone ios4
Jul 16 '10 at 21:21
source share
4 answers

You can change the volume during playback using the method described here:

http://developer.apple.com/library/ios/#qa/qa1716/_index.html

While the text of the article seems to suggest that it can only be used to mute the sound, you can set the volume to whatever you want, and you can set it after playing the sound. For example, if your AVAsset instance is called โ€œasset,โ€ your AVPlayerItem instance is called โ€œplayerItem,โ€ and the volume you want to install is called โ€œvolume,โ€ the following code should do what you want:

NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio]; NSMutableArray *allAudioParams = [NSMutableArray array]; for (AVAssetTrack *track in audioTracks) { AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters]; [audioInputParams setVolume:volume atTime:kCMTimeZero]; [audioInputParams setTrackID:[track trackID]]; [allAudioParams addObject:audioInputParams]; } AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix]; [audioMix setInputParameters:allAudioParams]; [playerItem setAudioMix:audioMix]; 
+21
May 30 '11 at 16:57
source share

Have you tried just preparing AVMutableAudioMix and installing it on AVPlayerItem while it is still playing? You should be able to request AVPlayer for your currentItem, which can provide its tracks, which should reference AVMutableAudioMixInputParameters for AVMutableAudioMix. The time you supply for the mixture refers to when the mixture is applied.

+4
Nov 19 '10 at 4:57
source share
 - (IBAction)sliderAction:(id)sender { NSLog(@"slider :%f ", self.mixerSlider.value); NSArray *audioTracks = [self.videoHandler.videoAsset tracksWithMediaType:AVMediaTypeAudio]; // Mute all the audio tracks NSMutableArray *allAudioParams = [NSMutableArray array]; for (AVAssetTrack *track in audioTracks) { AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters]; [audioInputParams setVolume:self.mixerSlider.value atTime:kCMTimeZero]; [audioInputParams setTrackID:[track trackID]]; [allAudioParams addObject:audioInputParams]; } AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix]; [audioMix setInputParameters:allAudioParams]; [[self.mPlayer currentItem] setAudioMix:audioMix]; } 

This method is called by changing the value of the slider. And you can call it during video playback.

+2
Aug 20 '13 at 15:49
source share

Please note that the AVMutableAudioMix method described in the original question (and several answers) only works with file assets, not streaming media.

This is described in detail in a document written by Apple here:

https://developer.apple.com/library/content/qa/qa1716/_index.html

When I tried to do this with streaming audio, I did not get any tracks returned from the AVAsset tracks(withMediaType:) (Swift) method.

0
Dec 14 '17 at 23:19
source share



All Articles