2016-11-02 3 views
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) 
      } 
     } 

    } 

입니다.

+0

[오류 코드는 파일에 문제가있는 것 같습니다.] (https://www.osstatus.com/search/results?platform=all&framework=all&search=1685348671) –

+0

오, 고맙습니다. 몰랐습니다. 이 웹 사이트에 대해! 그렇다면 Alamofire로 파일을 다운로드하고 mp3 파일로 저장하거나 대상 URL에서 파일을로드하는 방법은 무엇입니까? – sphynx

+1

코드에있는 내용이 나에게 잘못되었습니다. 파일이 유효한 mp3이고 404 페이지 또는 다른 파일이 아닌지 확인하십시오. –

답변

-1

잘못된 API URL을 입력 했으므로 재생할 수없는 잘못된 파일을 보냈습니다.

+0

다른 사람에게 도움이되지 않습니다. 정확히 무엇이 잘못되었는지 지정하십시오. 그렇지 않으면 대답이 용납되지 않습니다. – Martian2049