2016-11-03 1 views
0

Alamofire로 MP3 파일을 다운로드하려고합니다. 파일을 가져 와서 장치 저장 장치의 원하는 경로에 데이터 파일로 저장합니다 (mp3 확장명이 아님). 그리고 나서 AVAudioPlayer (AVPlayer로 시도)로 재생하려고하는데 성공하지 못했습니다. 해당 데이터 파일을 언 래핑하는 동안 nil을 발견했습니다. 이 파일은 크기가 7MB 이상이므로 유효하며 확장자가 .mp3 인 컴퓨터에서 재생할 수 있습니다.AVAudioPlayer가 URL 파일에서 nil을 찾습니다.

내 질문은 : 왜 파일이 ​​경로에서 유효하면 nil을 찾았습니까?

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())") 
    print(destinationUrl) 

    if FileManager.default.fileExists(atPath: destinationUrl.path) { 
     print("The song already exists") 
     do { 
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
      try AVAudioSession.sharedInstance().setActive(true) 

      let soundData = try Data(contentsOf: destinationUrl) 

      player! = try AVAudioPlayer(data: soundData) 

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

    } else { 
     print("Song was not found. Starting download proccess") 
     APIService.sharedService.downloadSong(song: song) { (success) in 
      if !success { 
       print("Failed to download the song") 
      } else { 
       print("Downloaded the song") 
      } 
     } 
    } 
} 

나는 URL, PlayerItem와 AVPlayer를 해봤 등 그래서 내가 어떻게 올바른 방법으로 해당 파일에 액세스해야합니까?

답변

0

나를위한 해결책을 찾았습니다. 데이터 오류가 아니 었습니다. 아무도 플레이어가 아닙니다. 그것은

player = try AVAudioPlayer(data: soundData) 

하지

player! = try AVAudioPlayer(data: soundData) 
로 선언되어야한다
관련 문제