如何让函数在 Swift 中播放不同的声音
Posted
技术标签:
【中文标题】如何让函数在 Swift 中播放不同的声音【英文标题】:How to make a function play different sounds in Swift 【发布时间】:2018-06-08 07:19:53 【问题描述】:我有 10 个不同的按钮,每个按钮都有一个与之关联的唯一标签。所有按钮都链接到同一个 IBAction。我具有根据与单击的特定按钮关联的标签播放不同声音的功能。
import AVFoundation
var myAudio: AVAudioPlayer!
let path = Bundle.main.path(forResource: "sound1", ofType: "wav")!
let url = URL(fileURLWithPath: path)
do
let sound = try AVAudioPlayer(contentsOf: url)
myAudio = sound
sound.play()
catch
//
【问题讨论】:
您的问题/问题是什么? 显示您的按钮操作代码 目前所有按钮播放相同的声音。我希望按钮根据单击的按钮播放不同的声音。 显示你尝试过的代码,给出答案很有用 我的声音播放代码写在上面。它包含在所有十个按钮的 IBAction 中。我想根据发送者按钮的标签更改“sound1”。 【参考方案1】:喜欢
@objc func yourbtnActionName(_ sender : UIButton)
switch sender.tag
case 1:
commonSoundCode(name: "sound1")
break
case 2:
commonSoundCode(name: "yoursecondSoundname")
break
default:
break
那么常用的方法为
func commonSoundCode(name: String)
let path = Bundle.main.path(forResource: name, ofType: "wav")!
let url = URL(fileURLWithPath: path)
do
let sound = try AVAudioPlayer(contentsOf: url)
myAudio = sound
sound.play()
catch
//
选项 2
如果你的声音文件的顺序相同,例如 (sound1.wav, sound2.wav......, sound10.wav) 然后调用类似
@objc func yourbtnActionName(_ sender : UIButton)
let path = Bundle.main.path(forResource: "sound\(sender.tag)", ofType: "wav")!
let url = URL(fileURLWithPath: path)
do
let sound = try AVAudioPlayer(contentsOf: url)
myAudio = sound
sound.play()
catch
//
【讨论】:
你应该避免使用0
标签,因为这是所有视图的默认值。
player.prepareToPlay() 如果我将这行代码添加到 player.play() 上方,它将如何影响我的代码
@UtkarshSharma - 看到这个寻求帮助,***.com/questions/25503629/…【参考方案2】:
你需要根据UIButton's
tag
简单地播放声音。
@IBAction func playsound(_ sender: UIButton)
switch sender.tag
case 0:
//Sound for Button with tag=0
case 1:
//Sound for Button with tag=1
default:
//Default Sound
在上面的代码中,根据你的UIButton
tags
添加更多的case。
请添加更多代码以使您的问题更加清晰。
【讨论】:
【参考方案3】:import UIKit
//Mark: Do import AVFoundation is must here!
import AVFoundation
class ViewController: UIViewController
//Mark: Create variable for AVAudioEngine! and AVAudioplayermode!
var engine: AVAudioEngine!
var audioPlayerNode : AVAudioPlayerNode!
//Mark: Create Variable for each tone as AVAudiofile!
var Cs:AVAudioFile!
var Ds:AVAudioFile!
var Es:AVAudioFile!
var Fs:AVAudioFile!
var Gs:AVAudioFile!
var As:AVAudioFile!
var Bs:AVAudioFile!
override func viewDidLoad()
super.viewDidLoad()
// Mark: Load engine
engine = AVAudioEngine()
// Mark : Mention each sound
Cs = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
Bundle.main.path(forResource: "C", ofType: "wav")!))
Ds = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
Bundle.main.path(forResource: "D", ofType: "wav")!))
Es = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
Bundle.main.path(forResource: "E", ofType: "wav")!))
Fs = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
Bundle.main.path(forResource: "F", ofType: "wav")!))
Gs = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
Bundle.main.path(forResource: "G", ofType: "wav")!))
As = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
Bundle.main.path(forResource: "A", ofType: "wav")!))
Bs = try? AVAudioFile(forReading: NSURL.fileURL(withPath:
Bundle.main.path(forResource: "B", ofType: "wav")!))
//Mark: create function(playSound) for connect AVAudioEngine and AVAudioplayermode for playing audio when button clicked
func playSound(audioFile: AVAudioFile)
audioPlayerNode = AVAudioPlayerNode()
if(audioPlayerNode.isPlaying)
audioPlayerNode.stop()
if(engine.isRunning)
engine.stop()
engine.reset()
engine.attach(audioPlayerNode)
engine.connect(audioPlayerNode, to: engine.mainMixerNode, format: audioFile.processingFormat)
audioPlayerNode.scheduleFile(audioFile, at: nil, completionHandler: nil)
// Start the audio engine
engine.prepare()
try! engine.start()
audioPlayerNode.play()
//Mark: create required button for actions to connect function(playSound) and mention each sound to button
@IBAction func btnC(_ sender: UIButton)
playSound(audioFile: Cs)
@IBAction func btnD(_ sender: UIButton)
playSound(audioFile: Ds)
@IBAction func btnE(_ sender: UIButton)
playSound(audioFile: Es)
@IBAction func btnF(_ sender: UIButton)
playSound(audioFile: Fs)
@IBAction func btnG(_ sender: UIButton)
playSound(audioFile: Gs)
@IBAction func btnA(_ sender: UIButton)
playSound(audioFile: As)
@IBAction func btnB(_ sender: UIButton)
playSound(audioFile: Bs)
【讨论】:
以上是关于如何让函数在 Swift 中播放不同的声音的主要内容,如果未能解决你的问题,请参考以下文章