AVAudioPlayer无法播放音频Swift [英] AVAudioPlayer not playing audio Swift

查看:198
本文介绍了AVAudioPlayer无法播放音频Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试播放声音作为对我的应用程序用户的警报,并且我尝试了一些方法来帮助我做到这一点:

I am attempting to play a sound as an alert to the user of my app and I've looked at a few sources in trying to help me do this:

AVAudioPlayer无法在Swift中播放音频(以帮助我解决问题我现在遇到了,无济于事

AVAudioPlayer not playing audio in Swift (to help me solve the problem I am running into now, to no avail)

快速创建和播放声音(我从这里开始最初)

Creating and playing a sound in swift (where I started originally)

这些视频:

https://www.youtube.com/watch?v=Kq7eVJ6RSp8

https://www.youtube.com/watch?v=RKfe7xzHEZk

所有这些都没有给我想要的结果(声音无法播放).

All of which are not giving me the desired outcome (the sound doesn't play).

这是我的代码:

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",位于项目目录中.在上下文中,该方法在这里调用:

Currently I see "preparation success" and "sound play success" but no sound is played. The class I am implementing this in is an AVAudioPlayerDelegate and the file is called "3000.mp3" which is within the project directory. In context the method is called here:

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
    }

任何帮助将不胜感激.

Any help would be appreciated.

推荐答案

更新/解决方案:

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

So the problem was that audioPlayer would become deallocated before it would play the sound, to fix this I had to make it a property within the class instead of just creating an instance of it within the function. The updated code looks like this:

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

Optional reference within the property declarations of the class:

var audioPlayer : AVAudioPlayer?

利用audioPlayer的功能:

The function that utilizes the 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无法播放音频Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆