我有两个ViewController(ViewController和Settings)。如果关闭Switch,按钮(或应用程序中)的声音被关闭并打开,我怎么能这样做?
按钮声音代码:
@IBAction func SoundButton(_ sender: UIButton) {
let filename = "button-16"
let ext = "mp3"
if let soundUrl = Bundle.main.url(forResource: filename, withExtension: ext) {
var soundId: SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundUrl as CFURL, &soundId)
AudioServicesAddSystemSoundCompletion(soundId, nil, nil, { (soundId, clientData) -> Void in
AudioServicesDisposeSystemSoundID(soundId)
}, nil)
AudioServicesPlaySystemSound(soundId)
}
}
答案 0 :(得分:0)
为boolean
获取全局isSound
变量UISwitch
。当UISwitch
on
表示true
时,请设置变量isSound on
。现在,根据变量制作条件。
您可以使用AppDelegate
文件或头文件创建全局变量。 (.h file
)
if isSound == true || isSound == on {
// make sound on
} else {
// make sound off
}
答案 1 :(得分:0)
在我的代码中,我正在播放文本语音和管理
var speedd = AVSpeechSynthesizer()
var voicert = AVSpeechUtterance()
var Strdata = "This is Sound for Music"
override func viewDidLoad()
{
super.viewDidLoad()
voicert = AVSpeechUtterance(string:Strdata)
voicert.voice = AVSpeechSynthesisVoice(language: "en-US")
voicert.rate = 0.5
}
@IBAction func switchSound(_ sender: UISwitch)
{
if sender.isOn
{
speedd.speak(voicert)
}
else
{
speedd.stopSpeaking(at: AVSpeechBoundary.immediate)
}
}