2

There are plenty of questions asking how to set the volume of an AVPlayer, but how do you get the current volume of the player in iOS?

For example, I am trying to fade a song out from its current level. I could save the volume elsewhere and refer to it, but would rather read the value directly from the AVPlayer.

bendytree
  • 13,095
  • 11
  • 75
  • 91

3 Answers3

6

AVPlayer is contains one or more AVPlayerItem objects, and it is through these objects that you can get and set audio levels for media played by an AVPlayer. Head to the AVPlayerItem docs and look at the audioMix property, and also check out my answer to a slightly different question that should still provide some info.


Following up after your comment, this is (I think) how you would get the volume values from the - (BOOL)getVolumeRampForTime:(CMTime)time startVolume:(float *)startVolume endVolume:(float *)endVolume timeRange:(CMTimeRange *)timeRange method:

// Get your AVAudioMixInputParameters instance, here called audioMixInputParameters
// currentTime is the current playhead time of your media

float startVolume;
float endVolume;
CMTimeRange timeRange;

bool success = [audioMixInputParameters getVolumeRampForTime: currentTime
                                                 startVolume: &startVolume
                                                   endVolume: &endVolume
                                                   timeRange: &timeRange];

// startVolume and endVolume should now be set
NSLog(@"Start volume: %f | End volume: %f", startVolume, endVolume);
Community
  • 1
  • 1
  • So there's `- (void) getVolumeRampForTime:startVolume:endVolume:timeRange:` on `AVAudioMixInputParameters` which comes from `player.audioMix.inputParameters[]`. Still not sure how to pull volume for the current time, but I'll post it when I figure it out. – bendytree Aug 23 '12 at 20:12
  • The `startVolume:` and `endVolume:` parameters of that method ask for pointers to floats, so that is the mechanism for extracting the volume values. –  Aug 23 '12 at 20:31
0

According to Apple's AVPlayer documentation for OS X, it lists a volume property, but the documentation for the same class in iOS doesn't show one listed. Would your project allow you to use AVAudioPlayer instead? That one does have a synthesized volume property for iOS that's much more easily set/retrieved.

  • Thanks Shaun but I am using the AVPlayer to stream a remote mp3 - which does not appear to be possible with the `AVAudioPlayer` – bendytree Aug 23 '12 at 19:38
-1

You could use the volume property of the AVPlayer class. Here's the AVPlayer class reference link. Quoting from it:

volume
Indicates the current audio volume of the player.

@property(nonatomic) float volume
Discussion
0.0 means “silence all audio,” 1.0 means “play at the full volume of the current item.”

Availability
Available in OS X v10.7 and later.

Declared In
AVPlayer.h

edit:

You could try geting the system volume instead. This link provides 2 ways.

Community
  • 1
  • 1
jhoanna
  • 1,797
  • 25
  • 25