在iOS中按下特定按钮时播放特定声音

我是iOS开发的初学者,我正在尝试创建一个简单的应用程序,当按下特定按钮并使用两个分段控件选项时,它会播放特定的声音(如下图所示)。 我已经在YouTube,堆栈和其他网站上查找过很多教程,但是所有这些教程似乎都给了我很多错误,或者对我来说太难理解了(因为我太缺乏经验)。

我的一些尝试,但这只工作1按钮和1声音。

import UIKit import AVFoundation class ViewController: UIViewController { // make sure to add this sound to your project var pianoSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("C", ofType: "m4a")) var audioPlayer = AVAudioPlayer() override func viewDidLoad() { super.viewDidLoad() audioPlayer = AVAudioPlayer(contentsOfURL: pianoSound, error: nil) audioPlayer.prepareToPlay() } @IBAction func PianoC(sender: AnyObject) { audioPlayer.play() } } 

请有人告诉我如何制作这个应用程序,如下图所示?

http://imgur.com/tWgCDWD

必需:有三个按钮(猫,狗,鸟),每个按钮都有自己的声音。 以及用于启用或禁用声音的第一个分段(声音),例如。 当用户选择声音关闭选项时,即使单击按钮也没有声音,第二个分段用于启用或禁用背景音乐。

因此,创建一个按名称播放声音的方法,并让每个按钮动作调用该方法。 还有每种方法在开始播放声音之前检查播放声音开关的状态。

 @IBOutlet weak playSoundSwitch: UISwitch! var backgroundMusicPlayer: AVAudioPlayer? @discardableResult func playSound(named soundName: String) -> AVAudioPlayer { var soundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(soundName, ofType: "m4a")) audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil) ( it MUST be a class variable ) audioPlayer.play() return audioPlayer } @IBAction func catButtonPressed(sender: AnyObject) { if playSoundSwitch.isOn { playSound("catSound") } } @IBAction func dogButtonPressed(sender: AnyObject) { if playSoundSwitch.isOn { playSound("dogSound") } } @IBAction func birdButtonPressed(sender: AnyObject) { if playSoundSwitch.isOn { playSound("birdSound") } } @IBAction func playBackgroundMusicSwitchChanged(sender: UISwitch) { if sender.isOn { backgroundMusicPlayer = playSound("backgroundSound") } else { backgroundMusicPlayer?.stop() backgroundMusicPlayer = nil } } 

以上假设您的背景音乐声音足够长,可以持续整个会话。 如果你需要在每次结束时重复声音,那将会更复杂。 您必须将自己设置为声音播放器的代理,并在每次结束时重新启动它。 您将使用audioPlayerDidFinishPlaying(_:successfully:)委托方法。

如果您希望播放声音开关提前终止当前声音,则需要添加逻辑来处理它。 同样,如果你想一次只允许一个动物声音,你必须为此添加逻辑。

(上面的代码在SO编辑器中被删除了。它可能包含需要清理的小语法错误。它只是作为指南,而不是复制/粘贴代码。)

最新语法..

 import AVFoundation class yourClass { var _sfx: AVAudioPlayer? // IT >MUST< BE a class variable func sound(named: String) { let u = Bundle.main.path(forResource: named, ofType: "m4a")! let s = URL(fileURLWithPath:u) do { _sfx = try AVAudioPlayer(contentsOf: s) _sfx.play() // note, you >CAN NOT< say here .. // let variable = try AVAudioPlayer(contentsOf: s) // IT >MUST< BE a class variable } catch { print("WOE!"); return } print("PLAYED \(named)") } 
 import UIKit import AVFoundation class ViewController: UIViewController { @IBOutlet weak var playSoundSwitch: UISegmentedControl! var backgroundMusicPlayer: AVAudioPlayer? var player:AVAudioPlayer = AVAudioPlayer() @discardableResult func playSound(named soundName: String) -> AVAudioPlayer { let audioPath = Bundle.main.path(forResource: soundName, ofType: "wav") player = try! AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL) player.play() return player } @IBAction func catButtonPressed(_ sender: Any) { if playSoundSwitch.selectedSegmentIndex == 0 { playSound(named: "catSound") } } @IBAction func dogButtonPressed(_ sender: Any) { if playSoundSwitch.selectedSegmentIndex == 0 { playSound(named: "dogSound") } } @IBAction func birdButtonPressed(_ sender: Any) { if playSoundSwitch.selectedSegmentIndex == 0 { playSound(named: "birdSound") print("bird sound") } } @IBAction func playBackgroundMusicSwitchChanged(_ sender: Any) { if (sender as AnyObject).selectedSegmentIndex == 0 { backgroundMusicPlayer = playSound(named: "backgroundSound") } else { backgroundMusicPlayer?.stop() backgroundMusicPlayer = nil } } }