简体   繁体   English

Swift检查AVAudioPlayer是否正在崩溃

[英]Swift checking if AVAudioPlayer is playing crash

I am making a soundboard with Swift. 我正在用Swift制作音板。 It works great but I am trying to impement a check whether AVAudioPlayer instance is playing, and if yes to stop it. 它工作得很好,但我试图检查AVAudioPlayer实例是否正在播放,如果是,则停止它。 The sounds are listed on a tableview which gets the data from an array in which I store my Sound class (which has 2 variables, title and url of sound). 声音列在tableview上,它从我存储Sound类的数组中获取数据(其中包含2个变量,标题和声音url)。

I have my AVAudioPlayer and AVAudioSession as well as my sounds array on my ViewController Class: 我有我的AVAudioPlayer和AVAudioSession以及我的ViewController类上的声音数组:

 var session = AVAudioSession.sharedInstance()
 var audioPlayer = AVAudioPlayer()
 var sounds: [Sound] = []

and I implement the audio playing when the app user selects a row like that: 当app用户选择这样的行时,我实现音频播放:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            if audioPlayer.playing {

                audioPlayer.stop()
            } else {
            var sound = self.sounds[indexPath.row]

            var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
            var pathComponents = [baseString, sound.url]
            var audioNSURL = NSURL.fileURLWithPathComponents(pathComponents)

            self.audioPlayer = AVAudioPlayer(contentsOfURL: audioNSURL, error: nil)
            self.audioPlayer.play()

        }
            tableView.deselectRowAtIndexPath(indexPath, animated: true)

    }

But when I tap the row the app is crashing and all I am getting is "(lldb)" 但是当我点击该行时,应用程序崩溃了,我得到的只是“(lldb)”

Do you have any idea what is happening? 你知道发生了什么吗? Or am I doing this completely wrong? 或者我这样做完全错了?

Thank you in advance 先感谢您

I figured it out, so I share with the community: 我想通了,所以我与社区分享:

I also had to declare the sound NSURL property on my ViewController Class : 我还必须在我的ViewController类上声明声音NSURL属性:

var session = AVAudioSession.sharedInstance()
var audioPlayer = AVAudioPlayer()
var audioNSURL = NSURL()

and also to prepare my audioPlayer on the ViewDidLoad function: (I had to use a sample sound in order to initialize the AVAudioPlayer) 并且还要在ViewDidLoad函数上准备我的audioPlayer :(我必须使用示例声音来初始化AVAudioPlayer)

let samplePath = NSBundle.mainBundle().pathForResource("sample", ofType: "mp4")
audioNSURL = NSURL.fileURLWithPath(samplePath!)!
audioPlayer = AVAudioPlayer(contentsOfURL: audioNSURL, error: nil)
audioPlayer.prepareToPlay()

then, on the didSelectRowAtIndexPath method, I check whether the AVAudioPlayer instance is playing and also if it's playing the sound reflected by the cell that was tapped. 然后,在didSelectRowAtIndexPath方法中,我检查AVAudioPlayer实例是否正在播放,以及它是否正在播放被轻击的单元格反射的声音。 If yes, stop the audioPlayer, if not then play the other sound (the audioPlayer stops and plays the other sound) 如果是,请停止audioPlayer,如果没有,则播放其他声音(audioPlayer停止并播放其他声音)

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var sound = self.sounds[indexPath.row]
        var baseString : String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
        var pathComponents = [baseString, sound.url]

        var rowSoundURL = NSURL.fileURLWithPathComponents(pathComponents)!


        if audioPlayer.playing && rowSoundURL == audioNSURL {

            audioPlayer.stop()

        } else {

            audioNSURL = rowSoundURL

            self.audioPlayer = AVAudioPlayer(contentsOfURL: audioNSURL, error: nil)
            self.audioPlayer.play()

        }

            tableView.deselectRowAtIndexPath(indexPath, animated: true)

    }

Be aware, that, if you also record sounds on your app, you have to set the session category as AVAUdioSessionCategoryPlayback or the sound will be heard through the small speaker of the device. 请注意,如果您还在应用上录制声音,则必须将会话类别设置为AVAUdioSessionCategoryPlayback,否则将通过设备的小型扬声器听到声音。 So, in viewWillAppear function: 那么,在viewWillAppear函数中:

session.setCategory(AVAudioSessionCategoryPlayback, error: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM