我把歌放进桌子里,我想当第一首歌唱完后,在桌子上播放下一首歌。>>
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let audio=NSBundle.mainBundle().pathForResource( elmp3[indexPath.row] , ofType: "mp3")
{
let url=NSURL (fileURLWithPath: audio)
do {
player = try AVAudioPlayer (contentsOfURL: url)
}
catch{"erorr"}当你按下塞尔歌曲时,播放同样的视图。
这是数组歌曲
var elmp3 = ["001","002","003","004","005","006","007","008","009","010","011","012","013","014","015","016","017","018","019","020","021","022","023","024","025","026","027","028","029"]发布于 2016-01-18 22:41:14
以下是以下步骤:
1.
var selectedIndex:Int = 02.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//Set selected index
self.selectedIndex = indexPath.row
//get audio file URL at index
if let audio=NSBundle.mainBundle().pathForResource( elmp3[self.selectedIndex] , ofType: "mp3")
{
let url=NSURL (fileURLWithPath: audio)
do {
player = try AVAudioPlayer (contentsOfURL: url)
//Set delegate for AVAudioPlayer
player.delegate = self
}
catch{"error"}
}class AudioPlayerController:UIViewController, AVAudioPlayerDelegate{
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer,
successfully flag: Bool){
if flag == true && self.selectedIndex < elmp3.count{
//Increment current index
++ self.selectedIndex
//Load audio at current index and play
if let audio=NSBundle.mainBundle().pathForResource( elmp3[self.selectedIndex] , ofType: "mp3")
{
let url=NSURL (fileURLWithPath: audio)
do {
player = try AVAudioPlayer (contentsOfURL: url)
player.delegate = self
}
catch{"error"}
}
}
}https://stackoverflow.com/questions/34865212
复制相似问题