【发布时间】:2016-12-10 19:23:44
【问题描述】:
我正在尝试向我的应用用户播放声音作为提醒,我查看了一些资源来帮助我做到这一点:
AVAudioPlayer not playing audio in Swift(帮我解决我现在遇到的问题,无济于事)
Creating and playing a sound in swift(我最初开始的地方)
还有这些视频:
https://www.youtube.com/watch?v=Kq7eVJ6RSp8
https://www.youtube.com/watch?v=RKfe7xzHEZk
所有这些都没有给我想要的结果(声音不播放)。
这是我的代码:
private func playFinishedSound(){
if let pathResource = NSBundle.mainBundle().pathForResource("3000", ofType: "mp3"){
let finishedStepSound = NSURL(fileURLWithPath: pathResource)
var audioPlayer = AVAudioPlayer()
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: finishedStepSound)
if(audioPlayer.prepareToPlay()){
print("preparation success")
audioPlayer.delegate = self
if(audioPlayer.play()){
print("Sound play success")
}else{
print("Sound file could not be played")
}
}else{
print("preparation failure")
}
}catch{
print("Sound file could not be found")
}
}else{
print("path not found")
}
}
目前我看到“准备成功”和“声音播放成功”,但没有播放声音。我在其中实现的类是一个 AVAudioPlayerDelegate,该文件名为“3000.mp3”,位于项目目录中。在上下文中,该方法在这里被调用:
private func finishCell(cell: TimerTableViewCell, currentTimer: TimerObject){
currentTimer.isRunning = false
cell.label.text = "dismiss"
cell.backgroundColor = UIColor.lightMintColor()
if(!currentTimer.launchedNotification){
playFinishedSound()
}
currentTimer.launchedNotification = true
}
任何帮助将不胜感激。
【问题讨论】:
-
还应该注意的是,我导入了
AVFoundation,并在“Build Phases”项目选项卡的“Link Binary With Libraries”选项中添加了AVFoundation.framework。
标签: ios swift avaudioplayer