2017-08-18 31 views
0

我有两个ViewController(ViewController和Settings)。我怎么能做到这一点,当关闭开关的按钮(或在应用程序)的声音被关闭和打开?按钮开/关设置中的声音

代码按钮的声音:

@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) 
    } 
} 

enter image description here

+0

很抱歉,但我不明白的问题,可以更详细地描述? –

+0

@AnhPham当开关关闭时,必须关闭按钮的声音 – KVL

回答

0

采取全球boolean变量isSoundUISwitch。当UISwitchon表示true时,使变量isSound on。现在,基于变量创建条件。

您可以用AppDelegate文件或头文件制作一个全局变量。 (.h file

if isSound == true || isSound == on { 
// make sound on 
} else { 
// make sound off 
} 
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) 
       } 

      }