添加声音时出现 NSBundle.mainBundle 错误
Error with NSBundle.mainBundle when adding sound
我正在尝试创建一个简单的音板,以便在按下按钮时发出声音,但是当我使用此代码时
let url = NSURL(fileURLWithPath: Bundle.mainBundle().pathForResource(sound, ofType: "mp3")!)
let audioPlayer = try AVAudioPlayer(contentsOfURL: url)
我收到 "Bundle.mainBundle" 的错误消息,最初它告诉我将 "Bundle" 从 "NSBundle" 更改为 "Bundle",因为它已被重命名,但是当我更正它时,我收到另一个错误消息 [=26] =]
我的整个代码:
import UIKit
import AVFoundation
class ViewController: UIViewController {
let soundFilenames = ["60gs", "check", "dada", "danceforme", "eat", "gods", "irelandbaby", "ko'd", "lefthand", "littlewerp", "nocalls", "precision", "sundaymorning", "surprise", "whothefuckisthatguy", "youlldonothing"]
var audioPlayers = [AVAudioPlayer]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for sound in soundFilenames {
do {
let url = Bundle.main.url(forResource:sound, withExtension: "mp3")!
let audioPlayer = try AVAudioPlayer(contentsOfURL: url)
audioPlayer.append(audioPlayer())
}
catch {
audioPlayers.append(AVAudioPlayer())
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonTapped(_ sender: UIButton) {
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.play()
}
}
谁能帮我解决这个问题。
首先在 Swift 3 mainBundle()
变成了 main
但为什么不是真正的捷径:
let url = Bundle.main.url(forResource:sound, withExtension: "mp3")!
并且 init 方法已重命名为
let audioPlayer = try AVAudioPlayer(contentsOf: url)
并将下一行更改为:
audioPlayers.append(audioPlayer)
我正在尝试创建一个简单的音板,以便在按下按钮时发出声音,但是当我使用此代码时
let url = NSURL(fileURLWithPath: Bundle.mainBundle().pathForResource(sound, ofType: "mp3")!)
let audioPlayer = try AVAudioPlayer(contentsOfURL: url)
我收到 "Bundle.mainBundle" 的错误消息,最初它告诉我将 "Bundle" 从 "NSBundle" 更改为 "Bundle",因为它已被重命名,但是当我更正它时,我收到另一个错误消息 [=26] =]
我的整个代码:
import UIKit
import AVFoundation
class ViewController: UIViewController {
let soundFilenames = ["60gs", "check", "dada", "danceforme", "eat", "gods", "irelandbaby", "ko'd", "lefthand", "littlewerp", "nocalls", "precision", "sundaymorning", "surprise", "whothefuckisthatguy", "youlldonothing"]
var audioPlayers = [AVAudioPlayer]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for sound in soundFilenames {
do {
let url = Bundle.main.url(forResource:sound, withExtension: "mp3")!
let audioPlayer = try AVAudioPlayer(contentsOfURL: url)
audioPlayer.append(audioPlayer())
}
catch {
audioPlayers.append(AVAudioPlayer())
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonTapped(_ sender: UIButton) {
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.play()
}
}
谁能帮我解决这个问题。
首先在 Swift 3 mainBundle()
变成了 main
但为什么不是真正的捷径:
let url = Bundle.main.url(forResource:sound, withExtension: "mp3")!
并且 init 方法已重命名为
let audioPlayer = try AVAudioPlayer(contentsOf: url)
并将下一行更改为:
audioPlayers.append(audioPlayer)