无法使用avaudioplayer播放歌曲

时间:2017-09-30 19:04:09

标签: ios swift

我创建了音乐目录,并创建了音乐和插图文件夹。歌曲在音乐文件夹中,现在我正在尝试播放歌曲,但它显示无法加载文件

Log image

if FileManager.default.fileExists(atPath: currentAudioPath.path)
    {
        print("The file already exists at path")

        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try AVAudioSession.sharedInstance().setActive(true)
            if UIApplication.shared.canOpenURL(currentAudioPath) {
                print("found music directory")
            }else {
                print("did not find music directory")
            }
            do {
                // Get the directory contents urls (including subfolders urls)
                let directoryContents = try FileManager.default.contentsOfDirectory(at: self.MusicPath, includingPropertiesForKeys: nil, options: [])
                print(directoryContents)

                // if you want to filter the directory contents you can do like this:
                let mp3Files = directoryContents.filter{ $0.pathExtension == "mp3" }
                print("mp3 urls:",mp3Files)
                audioPlayer = try AVAudioPlayer(contentsOf: mp3Files.first!, fileTypeHint: AVFileTypeMPEGLayer3)
                let mp3FileNames = mp3Files.map{ $0.deletingPathExtension().lastPathComponent }
                print("mp3 list:", mp3FileNames)
            audioPlayer.prepareToPlay()
            audioPlayer.play()
            audioPlayer.delegate = self
            audioLength = audioPlayer.duration
            playerProgressSlider.maximumValue = CFloat(audioPlayer.duration)
            playerProgressSlider.minimumValue = 0.0
            playerProgressSlider.value = 0.0
            audioPlayer.prepareToPlay()
            showTotalSongLength()
            updateLabels()
            progressTimerLabel.text = "00:00"
            } catch {
                print(error.localizedDescription)
            }
        } catch let error as NSError {
            print("error: \(error.localizedDescription)")
        }
    }

日志图片 但是当资源中的文件成功播放时(nsbundle.main ............)

0 个答案:

没有答案