Play Sound only
import AVFoundation
let fileUrl = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound", ofType: "mp3")!)
aPlayer = try? AVAudioPlayer(contentsOfURL:fileUrl);
aPlayer.play();
aPlayer.pause()
//stop and reset to starting point
aPlayer.stop()
aPlayer.currentTime = 0
Play Sound with UI
import MediaPlayer
let p = MPMoviePlayerViewController(contentURL: fileUrl);
presentViewController(p, animated: true, completion: nil)
Record and Play Sound
import AVFoundation
var avRec:AVAudioRecorder!
var audioFileUrl:NSURL!
var avPlayer:AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
FileUrl = (NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.AllDomainsMask)[0] as NSURL)
.URLByAppendingPathComponent("rec")
avRec = try? AVAudioRecorder(URL:audioFileUrl, settings: [:])
avRec.prepareToRecord()
}
@IBAction func playRecBtnClicked(sender: AnyObject) {
avPlayer = try? AVAudioPlayer(contentsOfURL: audioFileUrl)
avPlayer.prepareToPlay()
avPlayer.play()
}
@IBAction func stopRecBtnClicked(sender: AnyObject) {
avRec.stop()
}
@IBAction func startRecBtnClicked(sender: AnyObject) {
avRec.record()
}