0
Alamofire로 MP3 파일을 다운로드하고 원하는 위치에 저장하려고합니다. 그런 다음 그 위치에서 그 파일을 재생하려고하지만 오류가 발생합니다. OSStatus 오류 1685348671 AVAudioPlayer
이
이는 재생 기능 내가 파일을 다운로드하고 저장 한 후 재생의 가장 좋은 방법을 알고 싶습니다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)")
}
}
}
입니다 다운로드 기능
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)
}
}
}
입니다.
[오류 코드는 파일에 문제가있는 것 같습니다.] (https://www.osstatus.com/search/results?platform=all&framework=all&search=1685348671) –
오, 고맙습니다. 몰랐습니다. 이 웹 사이트에 대해! 그렇다면 Alamofire로 파일을 다운로드하고 mp3 파일로 저장하거나 대상 URL에서 파일을로드하는 방법은 무엇입니까? – sphynx
코드에있는 내용이 나에게 잘못되었습니다. 파일이 유효한 mp3이고 404 페이지 또는 다른 파일이 아닌지 확인하십시오. –