1

Is there a way to set the AVAudioSession outputVolume?

    self.audioSession = AVAudioSession.sharedInstance()
    self.audioSession.setActive(true, error: nil)
    self.audioSession.addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.New, context: nil)
    self.audioSession.outputVolume

Currently I am able to access the outputVolume, but I am having troubles of finding a way to set it.

Thanks!

AustinT
  • 1,998
  • 8
  • 40
  • 63

2 Answers2

5

The outputVolume property is read-only. This is due to Apple's conviction that a good user experience will be broken if apps unexpectedly modify sound volume beyond user control.

From the AVAudioSession docs:

The system wide output volume can be set directly only by the user; to provide volume control in your app, use the MPVolumeView class

So this is your only (easy) option...

  MPVolumeView *myVolumeView =
    [[MPVolumeView alloc] initWithFrame:frame];
    [self.view addSubview: myVolumeView];
foundry
  • 31,615
  • 9
  • 90
  • 125
0

I was able to change the main volume using the following on iOS 8

#import <MediaPlayer/MediaPlayer.h>

[[MPMusicPlayerController applicationMusicPlayer] setVolume:0.0];
[[MPMusicPlayerController iPodMusicPlayer] setVolume:0.0];

for info, the code is called thru a Cordova pluggin. This effects the main volume (the device volume popup show the new volume when code is called)

Fafaman
  • 141
  • 10