Swift:设置AVAudioSession输出音量 [英] Swift: Setting AVAudioSession Output Volume

查看:213
本文介绍了Swift:设置AVAudioSession输出音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以设置AVAudioSession outputVolume?

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

当前,我能够访问outputVolume,但是我在寻找一种设置方式上遇到了麻烦.

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

谢谢!

推荐答案

outputVolume属性是只读的.这是由于Apple坚信,如果应用程序意外地将音量更改超出用户控制,则良好的用户体验将被破坏.

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.

来自AVAudioSession文档:

From the AVAudioSession docs:

系统范围的输出量只能由用户直接设置;要在您的应用中提供音量控制,请使用MPVolumeView类

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

这篇关于Swift:设置AVAudioSession输出音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆