OSStatus错误1685348671 AVAudioPlayer [英] OSStatus error 1685348671 AVAudioPlayer

查看:624
本文介绍了OSStatus错误1685348671 AVAudioPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Alamofire下载mp3文件并将其保存到所需位置。然后,从那个位置尝试播放该文件,但是它给了我一个错误。

I'm trying to download mp3 file with Alamofire and save it to desired location. Then, from that location, I'm trying to play that file, but it gives me an error.

这是下载功能

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)
            }
        }

    }

这是一个播放功能

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)")
            }
        }

    }

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

I would like to know the best practice of downloading the file and saving it and then playing it.

推荐答案

我发现出来,我输入了错误的API网址,因此它给了我无法播放的无效文件。

I found out, that I've entered the wrong API url, so it gave me invalid file which is not playable.

编辑:

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

The file from the backend was not a valid MP3 audio file, that's why the error occurred.

这篇关于OSStatus错误1685348671 AVAudioPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆