更新为Xcode 11的公共发行版后尝试播放歌曲时出错

时间:2019-10-02 13:05:49

标签: xcode avfoundation avaudioplayer swift5

我从事此项目已有一段时间,因此在我再次开始之前,我决定将Xcode从beta版本更新为Xcode 11的公开版本。第一次运行我的应用程序时未做任何更改,Xcode给了我一个错误。在更新xcode之前,此播放功能可以正常工作。

  

线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x48)

在线 audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: listOfTheSongs[indexPath], ofType: "mp3", inDirectory: "Songs For Lyric Hunt")!))

我尝试在线搜索,但找不到任何东西,有人可以帮我吗?

这是我的视图控制器的代码

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAudioPlayerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()




        prepareAudioPlayerForSong()

//        option1Label.text = option1
//        option2Label.text = option2
//        option3Label.text = option3
//        option4Label.text = option4
    option1 = option1Label.text ?? "error"
    option2 = option2Label.text ?? "error"
    option3 = option3Label.text ?? "error"
    option4 = option4Label.text ?? "error"

}

func prepareAudioPlayerForSong () {
    if Bundle.main.path(forResource: listOfTheSongs[indexPath], ofType: "mp3", inDirectory: "Songs For Lyric Hunt") != nil {
        do{
            audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath:  Bundle.main.path(forResource: listOfTheSongs[indexPath], ofType: "mp3", inDirectory: "Songs For Lyric Hunt")!))
            audioPlayer.prepareToPlay()
        }catch{
            print(error)
        }
    }else {
        print("")
        print("listOfTheSongs at index path does not exist")
    }

    correctSelection = Int.random(in: 1...4)
    option1Label.text = listOfTheSongs[Int.random(in: 0...12)]
    option2Label.text = listOfTheSongs[Int.random(in: 0...12)]
    option3Label.text = listOfTheSongs[Int.random(in: 0...12)]
    option4Label.text = listOfTheSongs[Int.random(in: 0...12)]

    correctOption = findCorrectOption()



}

func findIndexPathOfSong (nameOfSong name: String) -> Int {
    var x = 0
    var indexpath = 0
    for name in listOfTheSongs
    {
        if name == listOfTheSongs[indexpath] {
            break
        } else {
            indexpath = indexpath + 1
        }

    }
    return(indexpath)
}

func findCorrectOption () -> String {
    if correctSelection == 1 {
        refreshLabelData()
        return option1Label!.text!
    }else if correctSelection == 2 {
        refreshLabelData()
        return option2Label!.text!
    }else if  correctSelection == 3 {
        refreshLabelData()
        return option3Label!.text!
    }else {
        refreshLabelData()
        return option4Label!.text!
    }
}

func refreshLabelData () {


    option1 = option1Label!.text!
    option2 = option2Label!.text!
    option3 = option2Label!.text!
    option4 = option4Label!.text!
}
var correctSelection = 1
var indexPath = 0
var option1 = "Wow"
var option2 = "cant Breath"
var option3 = "Stiches"
var option4 = "alive"
var correctOption = "Stiches"
var audioPlayer = AVAudioPlayer()
var listOfTheSongs = ["badLiar", "cool", "eastSide", "fallinAllInYou", "hateHowMuchILoveYou", "inMyBlood", "letMeDownSlowly", "loveYourself", "panini", "rewriteTheStars", "stiches", "treatYouBetter", "wow"]



@IBAction func PlayButton(_ sender: Any) {

    if audioPlayer.isPlaying {
        audioPlayer.pause()
        indexPath = indexPath + 1

    } else {
        audioPlayer.play()
        print("should be playing")
    }

}
@IBOutlet weak var option1Label: UILabel!
@IBAction func option1Button(_ sender: Any) {
    refreshLabelData()
    if option1 == correctOption {
        label1.text = "YAY YOU WIN"
        audioPlayer.pause()
        indexPath = indexPath + 1

    } else {
        label1.text = "Not Quite"
    }
}
@IBOutlet weak var option2Label: UILabel!
@IBAction func option2Button(_ sender: Any) {
    if option2 == correctOption {
        refreshLabelData()
        label1.text = "YAY YOU WIN"
        audioPlayer.pause()
        indexPath = indexPath + 1

    }else {
        label1.text = "Not Quite"
    }

}
@IBOutlet weak var option3Label: UILabel!
@IBAction func option3Button(_ sender: Any) {
    if option3 == correctOption {
        refreshLabelData()
        label1.text = "YAY YOU WIN"
        audioPlayer.pause()
        indexPath = indexPath + 1

    }else {
        label1.text = "Not Quite"
    }

}
@IBOutlet weak var option4Label: UILabel!
@IBAction func option4Button(_ sender: Any) {
    if option4 == correctOption {
        label1.text = "YAY YOU WIN"
        audioPlayer.pause()
        indexPath = indexPath + 1
    }else {
        label1.text = "Not Quite"
    }

}
@IBOutlet weak var label1: UILabel!
@IBAction func swipedRightGestureRecognizer(_ sender: Any) {
    indexPath = indexPath + 1
    prepareAudioPlayerForSong()
    audioPlayer.play()

    correctSelection = Int.random(in: 1...4)
    option1Label.text = listOfTheSongs[Int.random(in: 0...12)]
    option2Label.text = listOfTheSongs[Int.random(in: 0...12)]
    option3Label.text = listOfTheSongs[Int.random(in: 0...12)]
    option4Label.text = listOfTheSongs[Int.random(in: 0...12)]

    correctOption = findCorrectOption()
}

}

0 个答案:

没有答案