我目前正致力于从Firebase下载.m4a音频格式文件的代码,并允许用户在同一个View Controller中听到音乐播放。通过对SO和其他网站的大量研究,我设法下载了音频,但是当音频应该被播放时,我只能听到静音。事实上,它实际上播放音频剪辑的整个持续时间(例如,音频是6s长,所以用户会听到6s的静态),所以我知道它实际上是在下载一些东西,只是没有播放。< / p>
以下是我的下载功能和播放音乐功能的源代码。我尝试在SO上搜索这个主题,但很少有关于从Firebase下载到iOS的音频的文章(大多数似乎是&#34;如何下载图像&#34;等等)
非常感谢您提前! 萨姆
//Function to start playing music
func playMusic(){
do {
self.audioPlay = try AVAudioPlayer(contentsOf: self.localSongURL)
self.audioPlay.delegate = self
self.audioPlay.prepareToPlay()
self.audioPlay.play()
self.audioPlay.volume = 1.0 //I tried adjusting volumes to see if it would make a difference
} catch {
createAlert(title: "File Not Found", message: "Audio downloaded cannot be interpereted.")
}
}
//Function to download music
//In the Firebase storage, songs are listed underneath an ID, so the "tillAt"s help to parse through
//the appropriate strings to get the right path for collection
func downloadMusic(){
let tillAt1 = self.songPath.components(separatedBy: ID + "/")
let tillAt2 = tillAt1[1].components(separatedBy: ".")
store = Storage.storage().reference().child(patientID).child(tillAt1[1])
//From here is where I think the issue is within
self.localSongURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent(tillAt2[0] + ".m4a")
store.getData(maxSize: 128 * 1024 * 1024, completion: {(data, error) in
if let error = error {
print("Error Here _______________ Level 1")
print(error)
} else {
if let d = data {
do {
try d.write(to: self.localSongURL)
} catch {
print("Error Here _______________ Level 2")
print(error)
}
}
}
})
}