如何在AVAudioPlayer中从Core Data播放NSData

时间:2015-09-10 19:42:59

标签: ios swift nsdata avaudioplayer

我将mp3文件保存为Core Data作为NSData二进制数据。然后我在NSFetchedResultsController的帮助下加载此文件。然后我写了下面的代码,但我得到了错误。我该如何解决?

  

致命错误:在解包可选值时意外发现nil

var audioPlayer = AVAudioPlayer()
func play() {
    if musicData != nil {
        audioPlayer = AVAudioPlayer(data: musicData, error: nil)
        AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
        AVAudioSession.sharedInstance().setActive(true, error: nil)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }
}

更新 我检查过它,我看到AVAudioPlayer是零,但我在这节课的开头就是这个玩家。

我仍然改变了代码,但是我得到了同样的错误 第二种方法

var av = AVAudioPlayer()
func play() {
    let object = fetchedResultsController.fetchedObjects?.first as! Entity
    let songData = object.mp3

    var urlData: NSURL!
    songData.writeToURL(urlData, atomically: true)
    av = AVAudioPlayer(contentsOfURL: urlData, error: nil)
    av.prepareToPlay()
    av.play()
}

2 个答案:

答案 0 :(得分:0)

我添加了文件类型提示,它适用于我。

  func playMusic() {
        let url = NSBundle.mainBundle().URLForResource("Music", withExtension: "mp3")!
        let data = NSData(contentsOfURL: url)!
        AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
        AVAudioSession.sharedInstance().setActive(true, error: nil)
        audioPlayer = AVAudioPlayer(data: data, fileTypeHint: AVFileTypeMPEGLayer3, error: nil)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }

答案 1 :(得分:0)

播放来自Core Data的mp3文件时遇到相同的问题。还有另一种解决方案,它不需要您创建资源,而仅需要文件管理器。可在Swift 5中使用:

final class AudioPlayer: AVPlayer, ObservableObject {

    private func changeSong(with newSong: Song) { // struct Song has function findInDatabase, which can return saved data from CoreData (or error)

        // trying to find song in CoreData
        newSong.findInDatabase() { (result: Result<SavedSong, CoreDataError>) in

            switch result {
            case .success(let savedSong):
                // SavedSong struct has variable 'file' which is of type Data
                guard let songFile = savedSong.file, !songFile.isEmpty else {
                    // your error handling
                    return
                }

                guard var localURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else { return } // or error handling
                localURL.appendPathComponent("song.mp3")
                try! songFile.write(to: localURL) // saving song data in url

                // now just playing from created URL
                let nextPlayerItem = AVPlayerItem(url: localURL)
                self.replaceCurrentItem(with: nextPlayerItem) // 'self' - is AVPlayer instance
                self.play()

            case .failure( _):
                // error handling, for example try to play from URL
            }

        }

    } 

}