-4

I would like to activate a function after a sound is played and complete. I assume that NSTimer needs to be included, but I am not sure of how to implement in order to make it. It is helpful if you could give me some code that works.

1
  • 2
    Have you tried googling your issue? swift nstimer for example gives plenty of results. Commented Apr 24, 2016 at 13:34

1 Answer 1

5

You should post code that you tried, and then someone then can help you make the code work. In this case, you don't need a timer for what you're doing. The AVAudioPlayerDelegate protocol on the player was designed for what you are trying to do:

import UIKit import AVFoundation class ViewController: UIViewController, AVAudioPlayerDelegate { var audioPlayer:AVAudioPlayer? func playSound(fileName:NSString) { let path = NSBundle.mainBundle().pathForResource(fileName, ofType:"wav") let url = NSURL.fileURLWithPath(path!) do { try audioPlayer = AVAudioPlayer(contentsOfURL: url) audioPlayer?.delegate = self audioPlayer?.play() } catch { print("Player not available") } } //MARK : AVAudioPlayerDelegate func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) { print("finished playing, do something else here") } } 
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I would recommend as well. The AVAudioPlayerDelegate audioPlayerDidFinishPlaying(_, successfully:) method is how you should invoke code when a sound finishes playing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.