我一直在尝试编写一个音板应用程序,它当然有多个声音,到目前为止我有两个按钮和两个声音,但每个按钮播放相同的声音。我是一个菜鸟程序员,所以请详细向我解释我需要做什么。我为我的格式化道歉,因为这是我第一个被问到堆栈溢出的问题,下面的所有内容都是代码。我目前在故事板上有两个按钮。帮助将不胜感激。下面是我的ViewController.swift
import UIKit
import AVFoundation
class ViewController: UIViewController {
var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button", ofType: "wav")!)
var sound2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sweg", ofType: "wav")!)
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func soundbutton(sender: UIButton) {
audioPlayer.play()
}
@IBAction func sound2(sender: UIButton) {
audioPlayer.play()
}
}
答案 0 :(得分:1)
您应该使用单个IBAction来处理那些按钮
@IBAction func soundButton (sender: UIButton){
switch sender.currentTitle! {
case "sound1"://here you put the title the first button has
audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
audioPlayer.play()
case "sound2"://here you put the title the second button has,here sound2 was an example,because IDK your buttons titles
audioPlayer = AVAudioPlayer(contentsOfURL: sound2, error: nil)
audioPlayer.play()
default : break
}
}
currentTitle是按钮的标题,因此如果第一个按钮标题是,则将sound1替换为。
没有笑话表情符号工作:)):D
希望这有助于
<强> 修改 强>
你正在做的错误是你在viewDidload audioPlayer
中设置声音,这就是为什么你只有一个声音
您可以从viewDidLoad
中删除该行编辑:在课堂内 这是您在ViewController中所需要的,仅此而已(),确保您将按钮连接到IBAction并断开连接来自其他方法
import UIKit
import AVFoundation
class ViewController: UIViewController {
var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button", ofType: "wav")!)
var sound2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sweg", ofType: "wav")!)
var audioPlayer = AVAudioPlayer()
@IBAction func soundButton (sender: UIButton){
switch sender.currentTitle! {
case "sound1"://here you put the title the first button has
audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
audioPlayer.play()
case "sound2"://here you put the title the second button has,here sound2 was an example,because IDK your buttons titles
audioPlayer = AVAudioPlayer(contentsOfURL: sound2, error: nil)
audioPlayer.play()
default : break
}
}
}
最简单的实现
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
@IBAction func soundButton (sender: UIButton){
if let title = sender.currentTitle{
switch title {
case "sound1"://here you put the title the first button has
audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button", ofType: "wav")!), error: nil)
audioPlayer.play()
case "sound2"://here you put the title the second button has,here sound2 was an example,because IDK your buttons titles
audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sweg", ofType: "wav")!), error: nil)
audioPlayer.play()
default : break
}
}
}
}
现在唯一的错误是SIGABART,因为这些按钮有一些出口和动作
答案 1 :(得分:0)
你需要用另一个声音初始化另一个AVAudioPlayer,然后在第二个按钮上弹奏它。
答案 2 :(得分:0)
import UIKit
import AVFoundation
class ViewController: UIViewController {
var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button", ofType: "wav")!)
var sound2 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sweg", ofType: "wav")!)
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func soundbutton(sender: UIButton) {
audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
audioPlayer.play()
}
@IBAction func sound2(sender: UIButton) {
audioPlayer = AVAudioPlayer(contentsOfURL: sound2, error: nil)
audioPlayer.play()
}
}