0

I want to add the following method in for loop.

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)
        }

i add the above method in 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)
    }

The problem is that the loop runs very quicky and doesn't wait the avqueueplayer to finish the play. Add notification and change the code into the loop for waiting until the follow method call but the same problem.

func playerDidFinishPlaying(note: NSNotification) {

        flag=true
    }
palios7
  • 15
  • 3
  • Not sure, but on an AVPlayItem you can call use addObserver NSNotificationCenter.defaultCenter().addObserver(self, selector: "finishedPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem) to catch the first run of three, songs. Check your loop counter and relaunch it, do so 10 times. – user3069232 Feb 16 '16 at 12:24
  • its the same...i want the app notify me when finish the last song to run again the loop – palios7 Feb 16 '16 at 14:57
  • Take the playQueue out of a loop and tell it to play again, only when the player did finish is called. – user3069232 Feb 16 '16 at 17:43

0 Answers0