1
我試圖從Alamofire下載的數據文件播放MP3。這首歌很好,我可以用AVAudioPlayer播放(數據:數據)。我應該如何使用AVPlayer播放相同的數據文件?我找不到如何從Data創建PlayerItem或從Data創建AVAsset。 此外,我有歌曲路徑的網址,但該URL不能以某種方式與AVPlayer一起使用。音樂只是不玩。AVPlayerItem從URL不玩
func startSong(withIndex: Int) {
if let item = getItem(atIndex: withIndex) {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
print("AVAudioSession Category Playback OK")
do {
try AVAudioSession.sharedInstance().setActive(true)
print("AVAudioSession is Active")
} catch let error as NSError {
print(error.localizedDescription)
}
let playerItem = item
self.player = AVPlayer(playerItem: playerItem)
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
func getItem(atIndex: Int) -> AVPlayerItem? {
var item: AVPlayerItem
var url: URL
let song = playlist.getSong(index: atIndex)
if PlaylistsService.sharedService.isFileDownloaded(inPath: SongPath.Offline(id: (song?.getID())!).path()) {
url = URL(fileURLWithPath: SongPath.Offline(id: (song?.getID())!).path())
} else if PlaylistsService.sharedService.isFileDownloaded(inPath: SongPath.Temp(id: (song?.getID())!).path()) {
url = URL(fileURLWithPath: SongPath.Temp(id: (song?.getID())!).path())
} else {
self.downloadSong(song: song!)
url = URL(fileURLWithPath: SongPath.Temp(id: (song?.getID())!).path())
}
item = AVPlayerItem(url: url)
return item
}
@IBAction func playPause(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
print("Play")
self.player.play()
} else {
print("Pause")
self.player.pause()
}
}
在'self.player.play()'上放置一個斷點。這條線是否實際執行? – matt
是的。 http://prntscr.com/d4njv4 – sphynx
好的,在你說'self.player.play()'的地方,你可以嘗試記錄關於玩家狀態的更多信息。它的'currentTime'是什麼?它的'currentItem'是什麼?如果它有一個項目,該項目的「狀態」是什麼?那個項目的「資產」是什麼? – matt