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.
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];
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.
- (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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With