20

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.

SageAMDP
  • 441
  • 2
  • 6
  • 15

5 Answers5

25

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];
Jesse Crossen
  • 6,945
  • 2
  • 31
  • 32
  • Thanks for that link. I found a very important piece of information on the bottom of that page. "Note: AVAudioMix only supports file-based assets." I couldn't figure out why my fade was broken and now I know, because my initial testing was with local files and now they are streaming. It seems that there is no way to adjust the volume for streaming tracks using AVPlayer...? – SteveB Jun 07 '11 at 17:01
  • Interesting. It seems to be working with streamed media for me, so there's probably some other factor that has to be just so. I'd like to put together a simple test app at some point when I have the time. – Jesse Crossen Jun 13 '11 at 13:51
  • can you help me for this code `AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:songURL];` `[avPlayer play];` Do i need to use such long code when i have this simple lines.. – DShah Sep 17 '11 at 06:23
  • This thing works, but in older device such as iPod Touch (iOS 4.0), if I try to do this after manually seeking the player (fast forward/rewind) like this, `[player seekToTime:newTime];`, then this technique stops working. The volume does not change. Any idea why this is happening? – Bijoy Thangaraj Feb 17 '12 at 10:04
  • Reading documents, I get the idea that Apple don't want us to control volume over MPVolumeView. So do you think this code will be approved? – Bartu Mar 27 '14 at 14:02
  • This seems to work great on the device, but doesn't work over airplay. Thoughts? – tyler Jun 21 '14 at 18:02
4

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.

mmms
  • 41
  • 2
  • 1
    Just setting [[player currentItem] setAudioMix:audioMix]; (from a UISlider valueChanged action) is OK. – ıɾuǝʞ Nov 23 '11 at 11:26
3
- (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.

vatti
  • 463
  • 7
  • 16
2

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

Jeff C.
  • 2,031
  • 2
  • 17
  • 28
0

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.

christophercotton
  • 5,829
  • 2
  • 34
  • 49