首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在第一首歌结束时自动播放下一首歌

如何在第一首歌结束时自动播放下一首歌
EN

Stack Overflow用户
提问于 2016-01-18 22:25:59
回答 1查看 821关注 0票数 0

我把歌放进桌子里,我想当第一首歌唱完后,在桌子上播放下一首歌。>>

代码语言:javascript
复制
 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"}

当你按下塞尔歌曲时,播放同样的视图。

这是数组歌曲

代码语言:javascript
复制
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"]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-18 22:41:14

  1. 为selectedIndex创建一个实例变量。
  2. 在selectedIndex : method中将indexPath.row更新为indexPath.row。
  3. 实现AVAudioPlayerDelegate方法并为AVAudioPlayer实例设置委托。
  4. 在audioPlayerDidFinishPlaying:回调方法中,增加selectedIndex并在列表中播放下一个音频。

以下是以下步骤:

1.

代码语言:javascript
复制
var selectedIndex:Int = 0

2.

代码语言:javascript
复制
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"}

 }
代码语言:javascript
复制
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"}


}
}
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34865212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档