SageAMDP
SageAMDP

Reputation: 431

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;

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.

Upvotes: 20

Views: 22020

Answers (5)

christophercotton
christophercotton

Reputation: 5879

I have been able to use the volume property on the AVPlayer it is documented at:

/* Indicates the current audio volume of the player; 0.0 means "silence all audio", 1.0 means "play at the full volume of the current item".

   iOS note: Do not use this property to implement a volume slider for media playback. For that purpose, use MPVolumeView, which is customizable in appearance and provides standard media playback behaviors that users expect.
   This property is most useful on iOS to control the volume of the AVPlayer relative to other audio output, not for volume control by end users. */
@property float volume API_AVAILABLE(macos(10.7), ios(7.0), tvos(9.0), watchos(1.0));

You can just adjust it with:

   // to make it play at 50% of its full volume
   avplayer.volume = 0.5

As the documentation states, it doesn't change the system level, but the volume relative to the system. I have used this to mix two streams that were playing.

Upvotes: 0

Jeff C.
Jeff C.

Reputation: 2135

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).

Upvotes: 2

vatti
vatti

Reputation: 473

- (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.

Upvotes: 3

Jesse Crossen
Jesse Crossen

Reputation: 6995

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];

Upvotes: 25

mmms
mmms

Reputation: 41

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.

Upvotes: 4

Related Questions