首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OSStatus误差1685348671 AVAudioPlayer

OSStatus误差1685348671 AVAudioPlayer
EN

Stack Overflow用户
提问于 2016-11-02 20:49:06
回答 1查看 5.5K关注 0票数 2

我试图下载mp3文件与阿拉莫火,并将其保存到所需的位置。然后,从那个位置,我试着播放那个文件,但是它给了我一个错误。

这是下载功能

代码语言:javascript
复制
func downloadSong(song: Song, completion: @escaping(Bool) -> ()) {

        var headers: HTTPHeaders = [:]

        if let authorizationHeader = Request.authorizationHeader(user: Constants.authName, password: Constants.authKey) {
            headers[authorizationHeader.key] = authorizationHeader.value
        }

        let destination: DownloadRequest.DownloadFileDestination = { _, _ in
            let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
            let fileURL = documentsURL.appendingPathComponent("\(song.getID()).mp3")

            return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
        }

        Alamofire.download(APIEndpoint.Song(id: song.getID(), token: self.authToken!).path(), method: .get, headers: headers, to: destination).response { (response) in
            if response.error == nil, let filePath = response.destinationURL?.path {
                print(filePath)
                completion(true)
            } else {
                completion(false)
            }
        }

    }

这是一个回放功能

代码语言:javascript
复制
func playSong() {

        guard let songs = currentPlaylist?.getSongs() else { return }

        let song = songs[0]

        let documentsDirectoryURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

        let destinationUrl = documentsDirectoryURL.appendingPathComponent("\(song.getID()).mp3")
        print(destinationUrl)

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

            do {
                try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
                try AVAudioSession.sharedInstance().setActive(true)

                player! = try AVAudioPlayer(contentsOf: destinationUrl, fileTypeHint: AVFileTypeMPEGLayer3)

                player!.play()
            } catch let error as NSError {
                print("error: \(error.localizedDescription)")
            }
        }

    }

我想知道下载文件,保存文件,然后播放的最佳实践。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-03 14:43:14

我发现,我输入了错误的API url,所以它给了我不可播放的无效文件。

编辑:

后端的文件不是有效的MP3音频文件,这就是错误发生的原因。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40389522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档