Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting the volume of a playing AVPlayer

The only method for getting a volume change on currently playing AVPlayer items is to follow this process;

  • Offload the currently playing AVPlayerItem's asset
  • Grab the current playback time for that AVPlayerItem
  • Load the asset into a new AVPlayerItem
  • Replace the current AVPlayerItem with the new one
  • Wait for the currentItem to change on the AVPlayer
  • Prepare your AudioMix and seek to previous playback time

Have I missed a basic principle somewhere or is it meant to be this convoluted to simply manage volume levels?

I cannot use an AVAudioPlayer because I need to load iTunes tracks into the player.

like image 736
SageAMDP Avatar asked Jul 16 '10 21:07

SageAMDP


4 Answers

You can change the volume while playing 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 audio, you can actually set the volume to anything you like, and you can set it after the audio is playing. For example, assuming your instance of AVAsset is called "asset", your instance of AVPlayerItem is called "playerItem", and the volume you want to set 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];
like image 88
Jesse Crossen Avatar answered Nov 16 '22 00:11

Jesse Crossen


Have you tried just preparing an AVMutableAudioMix and setting it on the AVPlayerItem while it is still playing? You should be able to ask the AVPlayer for its currentItem, which can provide its tracks that the AVMutableAudioMixInputParameters for the AVMutableAudioMix should reference. The times that you supply for the mix are relative to when the mix is applied.

like image 23
mmms Avatar answered Nov 15 '22 22:11

mmms


- (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 the slider value changed. And you can call it while the video is playing.

like image 2
vatti Avatar answered Nov 16 '22 00:11

vatti


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

This is detailed in a document written by Apple, here:

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

When I attempted to do this with streaming audio I did not receive any tracks returned from AVAsset's tracks(withMediaType:) method (Swift).

like image 2
Jeff C. Avatar answered Nov 16 '22 00:11

Jeff C.