想要调整iPhone的声音,必须通知这种方式调用系统的单例MPVolumeView调整它的子视图UISlider,这时会弹出音量的提示框(这是合理的,苹果不允许不给用户任何提示就更改了系统声音了)
/// 改变系统声音为 90%
public class func changeVolumeToMax() {
let volumeBig = MPVolumeView()
var slider: UISlider?
for view: UIView in volumeBig.subviews {
SLog(view.self.description)
SLog(view.className())
if view.className() == "MPVolumeSlider" {
slider = view as? UISlider
break
}
}
let systemVolume = slider?.value ?? 0.5
if systemVolume < 0.9 {
slider?.setValue(0.9, animated: false)
slider?.sendActions(for: .touchUpInside)
}
}
录音或者语音识别的时候更改声音模式为:AVAudioSessionCategoryPlayAndRecord
/// 设置声音变小 - 录音的时候需要小
public class func volumeLittle() {
let session = AVAudioSession.sharedInstance()
if session.category != AVAudioSessionCategoryPlayAndRecord {
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try! session.setActive(true)
}
}
播放声音的时候更改声音模式为:AVAudioSessionCategoryPlayback,否则播放的声音很小
/// 设置声音变大 - 播放声音的时候需要大
public class func volumeBig() {
let session = AVAudioSession.sharedInstance()
if session.category != AVAudioSessionCategoryPlayback {
try! session.setCategory(AVAudioSessionCategoryPlayback)
try! session.setActive(true)
}
}
网友评论