Swift: Setting AVAudioSession Output Volume

后端 未结 2 1089
遥遥无期
遥遥无期 2021-01-15 17:25

Is there a way to set the AVAudioSession outputVolume?

    self.audioSession = AVAudioSession.sharedInstance()
    self.audioSession.setActive(true, error: n         


        
相关标签:
2条回答
  • 2021-01-15 18:10

    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];
    
    0 讨论(0)
  • 2021-01-15 18:15

    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)

    0 讨论(0)
提交回复
热议问题