4

I have AVPlayer Questions.

1.How to control the volume of it?

2.How to know if the AVPlayer is reloading music because bad connection, do i have some inidication of it?

Shay
  • 2,595
  • 3
  • 25
  • 35

1 Answers1

7

AVPlayer uses the system volume, so if you need to provide controls for this you can use MPVolumeView which gives you the slider for volume control.

For audio fading, you can use an AVAudioMix. Here's some code:

//given an AVAsset called asset...
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
id audioMix = [[AVAudioMix alloc] init];
id volumeMixInput = [[AVMutableAudioMixInputParameters alloc] init];

//fade volume from muted to full over a period of 3 seconds
[volumeMixInput setVolumeRampFromStartVolume:0 toEndVolume:1 timeRange:
     CMTimeRangeMake(CMTimeMakeWithSeconds(0, 1), CMTimeMakeWithSeconds(3, 1))];
[volumeMixnput setTrackID:[[asset tracks:objectAtIndex:0] trackID]];

[audioMix setInputParameters:[NSArray arrayWithObject:volumeMixInput]];
[playerItem setAudioMix:audioMix];

You can also abruptly set the volume for a mix at a given time with:

[volumeMixInput setVolume:.5 atTime:CMTimeMakeWithSeconds(15, 1)];

Hope this helps. This API is definitely not obvious. I'd highly recommend watching the WWDC 10 video entitled Discovering AV Foundation. It's excellent.

Ben Scheirman
  • 40,531
  • 21
  • 102
  • 137
  • 1
    just realized this only answers your volume question. Might be best to post that separately. – Ben Scheirman Jun 24 '11 at 20:59
  • Note that, per Apple, this method only works for _file_-based assets. https://developer.apple.com/library/content/qa/qa1716/_index.html – Jeff C. Dec 14 '17 at 23:33