Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

ios - Swift: Setting AVAudioSession Output Volume

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...