Search code examples
iphoneobjective-cipadiphone-5

AVPlayer volume control while playing Video


I am working on custom video Player using AVPlayer class now in that i want to implement volume control using with UISlidercontrol.

I have done this:

NSMutableArray *allAudioParams = [NSMutableArray array];
AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters];
[audioInputParams setVolume:volumeSlider.value atTime:kCMTimeZero];
[allAudioParams addObject:audioInputParams];
AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix];
[audioZeroMix setInputParameters:allAudioParams];
[self.playerItem setAudioMix:audioZeroMix];

But its not working for me so can anyone please suggest me any other solution for this i have done so much r&d but not able to find proper solution.

Thanks.


Solution

  • You can use below code to change volume :

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