MPMediaPickerController から取得した MPMediaItem を元に曲を再生する。
,AVAudioPlayerDelegate
func setPlay(item: MPMediaItem) {
let url: NSURL = item.value(forProperty: MPMediaItemPropertyAssetURL) as! NSURL
do {
audioPlayer = try AVAudioPlayer(contentsOf: url as URL, fileTypeHint: nil)
audioPlayer.delegate = self
audioPlayer.play()
} catch {
print(error)
}
}
func setStop(){
if(audioPlayer != nil){
audioPlayer.stop()
}
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool){
//曲の終了イベント
}