【问题标题】:AVAudioPlayer not playing audio SwiftAVAudioPlayer 不播放音频 Swift
【发布时间】: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


【解决方案1】:

更新/解决方案:

所以问题是 audioPlayer 在播放声音之前会被释放,为了解决这个问题,我必须将其设为类中的属性,而不是仅在函数中创建它的实例。更新后的代码如下所示:

类的属性声明中的可选引用:

var audioPlayer : AVAudioPlayer?

利用audioPlayer的函数:

private func playFinishedSound(){
        if let pathResource = NSBundle.mainBundle().pathForResource("3000", ofType: "mp3"){
            let finishedStepSound = NSURL(fileURLWithPath: pathResource)
            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")
        }
    }

【讨论】:

  • 为什么要两次实例化AVAudioPlayer(一个在外面do..catch,另一个在里面)?
  • @AlvinfromDiaspar 我不记得为什么,但 audioPlayer 否则会抛出错误......我现在无法查看代码,因为它位于私人仓库中,我不再有权访问到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多