Swift 3:AVAudioPlayer不播放声音
我有一个带音频播放器的结构:Swift 3:AVAudioPlayer不播放声音,swift,avplayer,Swift,Avplayer,我有一个带音频播放器的结构: struct MT_Audio { func playAudio(_ fileName:String, _ fileExtension:String, _ atVolume:Float) { var audioPlayer = AVAudioPlayer() if let audioPath = Bundle.main.path(forResource: fileName, ofType: fileExtension)
struct MT_Audio {
func playAudio(_ fileName:String, _ fileExtension:String, _ atVolume:Float) {
var audioPlayer = AVAudioPlayer()
if let audioPath = Bundle.main.path(forResource: fileName, ofType: fileExtension) {
let audioURL = URL(string:audioPath)
do {
audioPlayer = try AVAudioPlayer(contentsOf: audioURL!)
audioPlayer.volume = atVolume
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print(error)
}
}
}
}
//I'm calling it in viewDidLoad like this:
guard let fileURL = Bundle.main.url(forResource:"heartbeat-01a", withExtension: "mp3")
else {
print("can't find file")
return
}
let myAudioPlayer = MT_Audio() //<--RESOLVED THE ISSUE BY MAKING THIS A PROPERTY OF THE VIEWCONTROLLER
myAudioPlayer.playAudio("heartbeat-01a", "mp3", 1.0)
struct MT\u音频{
func playAudio(uFileName:String,fileExtension:String,Volume:Float){
var audioPlayer=AVAudioPlayer()
如果让audioPath=Bundle.main.path(forResource:fileName,of type:fileExtension){
让audioURL=URL(字符串:audioPath)
做{
audioPlayer=尝试AVAudioPlayer(内容:audioURL!)
audioPlayer.volume=atVolume
audioPlayer.prepareToPlay()
audioPlayer.play()
}抓住{
打印(错误)
}
}
}
}
//我在viewDidLoad中这样称呼它:
guard let fileURL=Bundle.main.url(用于资源:“heartbeat-01a”,扩展名为:“mp3”)
否则{
打印(“找不到文件”)
返回
}
让myAudioPlayer=MT_Audio()//看起来像您的audioPlayer
仅存储在您的playadio
函数中
尝试将audioPlayer
作为类中的变量,如下所示:
struct MT_Audio {
var audioPlayer: AVAudioPlayer?
mutating func playAudio(_ fileName:String, _ fileExtension:String, _ atVolume:Float) {
// is now member of your struct -> var audioPlayer = AVAudioPlayer()
if let audioPath = Bundle.main.path(forResource: fileName, ofType: fileExtension) {
let audioURL = URL(string:audioPath)
do {
let audioPlayer = try AVAudioPlayer(contentsOf: audioURL!)
audioPlayer.volume = atVolume
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print(error)
}
}
}
}
看起来您的audioPlayer
仅存储在playadio
功能中
尝试将audioPlayer
作为类中的变量,如下所示:
struct MT_Audio {
var audioPlayer: AVAudioPlayer?
mutating func playAudio(_ fileName:String, _ fileExtension:String, _ atVolume:Float) {
// is now member of your struct -> var audioPlayer = AVAudioPlayer()
if let audioPath = Bundle.main.path(forResource: fileName, ofType: fileExtension) {
let audioURL = URL(string:audioPath)
do {
let audioPlayer = try AVAudioPlayer(contentsOf: audioURL!)
audioPlayer.volume = atVolume
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print(error)
}
}
}
}
谢谢你的建议。仍然没有得到音频。不确定为什么将audioPlayer移动到结构中的全局变量会影响它是否播放音频。我在代码中检查了它,并将其实例化。文件路径正确。在Xcode中选择时,mp3文件将播放。这将成为grrrrr的一个早晨。工作正常了,你让我走上了正确的轨道-请参阅我上面的评论和对代码的更改。谢谢你的建议。仍然没有得到音频。不确定为什么将audioPlayer移动到结构中的全局变量会影响它是否播放音频。我在代码中检查了它,并将其实例化。文件路径正确。在Xcode中选择时,mp3文件将播放。这将成为grrrrr的一个早晨。让它工作起来,你把我放在正确的轨道上-查看上面我的评论和对代码的更改。看看Xcode中的右侧面板。选择文件后,应检查目标成员资格。如果没有,请检查并重试。我在发布之前已经检查过了。但是谢谢你的建议。请注意,你应该避免使用音频URL中的隐式展开可选代码>。Bundle.main提供了一个url方法,该方法返回一个可选的url,如果让audioUrl=Bundle.main.url(forResource:fileName,withExtension:fileExtension)
查看Xcode中的右面板,则使用该方法。选择文件后,应检查目标成员资格。如果没有,请检查并重试。我在发布之前已经检查过了。但是谢谢你的建议。请注意,你应该避免使用音频URL中的隐式展开可选代码>。Bundle.main提供了一个url方法,该方法返回一个可选的url,如果让audioUrl=Bundle.main.url(forResource:fileName,withExtension:fileExtension),则使用该url代替