gpt4 book ai didi

swift - 音板 Swift 2

转载 作者:行者123 更新时间:2023-11-30 13:21:10 25 4
gpt4 key购买 nike

我正在尝试用 swift 制作一个音板应用程序,四个按钮中的 3 个按钮使应用程序崩溃,另一个按钮不执行任何操作。我究竟做错了什么?我是编程新手,我试图按照教程进行操作,但它不起作用。

import AVFoundation
import UIKit

class FirstViewController: UIViewController {

let soundFilesname = ["Tacos", "What's that supposed to mean", "Cube fist man", "giraffe"]

var audioPlayers = [AVAudioPlayer]()

override func viewDidLoad() {
super.viewDidLoad()

//set up audio player
for sound in soundFilesname {

do{

//try and do somthing
let url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(sound, ofType: "mp3")!)
let audioPlayer = try AVAudioPlayer(contentsOfURL: url)

audioPlayers.append(audioPlayer)
}
catch{
//catch error that is thrown
audioPlayers.append(AVAudioPlayer())
}
}
}

@IBAction func buttonTapped(sender: AnyObject)
{
//get the audioplayer thats corresponds to the button pressed

let audioPlayer = audioPlayers [sender.tag]
audioPlayer.play()
}
}

更新

在我的 iPhone 5 IOS 9.0.2 上通过 Xcode 运行应用程序:

IOS模拟器:

最佳答案

请提供您点击按钮时收到的崩溃信息。

另外,在buttonTapped中打印以检查正确的标签号,可能是您正在访问数组中的越界元素,但除非您提供崩溃信息,否则不确定。

@IBAction func buttonTapped(sender: AnyObject)
{
// to check correct tag no
print("Button tag is \(sender.tag)")

//get the audioplayer thats corresponds to the button pressed
let audioPlayer = audioPlayers [sender.tag]
audioPlayer.play()
}

关于swift - 音板 Swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37759056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com