2016-11-02 121 views
0

Alamofireでmp3ファイルをダウンロードしようとしています。希望する場所に保存しようとしています。それから、その場所から、私はそのファイルを再生しようとしているが、それは私にエラーを与える。OSStatusエラー1685348671 AVAudioPlayer

これは

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

    } 

です。

+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

関連する問題