For循环中的Avqueueplayer

时间:2016-02-16 11:44:44

标签: swift for-loop avqueueplayer

我想在for循环中添加以下方法。

func playQuequeSounds(time:String){


        let  song1 = AVPlayerItem(URL:  NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("estimatedTime", ofType: "mp3", inDirectory: "Audiofiles")!))


        let song2 = AVPlayerItem(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(time, ofType: "mp3", inDirectory: "Audiofiles")!))

        let song3 = AVPlayerItem(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("minutes", ofType: "mp3", inDirectory: "Audiofiles")!))


        self.queueplayer = AVQueuePlayer(items: [song1,song2,song3])

        self.queueplayer.actionAtItemEnd = AVPlayerActionAtItemEnd.Advance

        self.queueplayer.play()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification , object: song3)
        }

我在viewWillApear

中添加了上述方法
override func viewWillAppear(animated: Bool) {
    voiceRouteTableView.reloadData()
     NSNotificationCenter.defaultCenter().removeObserver(self)




        for i in 0...voicesRoutes.count{

        flag=false
        self.playQuequeSounds(String(i+1))
        while flag==false{

        }


    }


        sleep(2)
    }

问题是循环运行非常快,并且不等待avqueueplayer完成游戏。添加通知并将代码更改为循环,等待以下方法调用,但同样的问题。

func playerDidFinishPlaying(note: NSNotification) {

        flag=true
    }

0 个答案:

没有答案