【发布时间】:2015-04-18 14:41:00
【问题描述】:
我一直在尝试编写一个音板应用程序,其中当然有多个声音,到目前为止,我有两个按钮和两个声音,但每个按钮播放相同的声音。我是一个菜鸟程序员,所以请详细解释一下我需要做什么。我为我的格式道歉,因为这是我被问到堆栈溢出的第一个问题,下面的所有内容都是代码。我的故事板上目前有 2 个按钮。帮助将不胜感激。下面是我的 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()
}
}
【问题讨论】:
-
在视图加载后删除该行只会导致我的应用程序在点击按钮时崩溃,您的第一个解决方案给了我一个错误:“二进制运算符'~='不能应用于'类型的操作数String' 和 'String?'" 在 "case" 行上。
-
查看截图