2017-08-18 11 views
0

私は2つの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

UISwitchのためのグローバル変数booleanisSoundしてください。 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) 
       } 

      } 
関連する問題