swift controller for the app
//
// PlaySoundViewControlerViewController.swift
// Record Teen
//
// Created by abdelrahman mohamed on 7/16/15.
// Copyright (c) 2015 abdelrahman mohamed. All rights reserved.
//
import UIKit
import AVFoundation
class PlaySoundViewControlerViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
var isPlaying = false
var timer = NSTimer()
var secondsTime = 0
var audioLengthSeconds: Float = 0.0
@IBOutlet weak var audioPlayerBar: UISlider!
@IBOutlet weak var timeViewer: UILabel!
func nsTimerToSeconds(NSTime: NSTimeInterval) -> Int{
return Int(NSTime) % 60
}
override func viewDidLoad() {
super.viewDidLoad()
// handling the audio attachment
var filePath = NSBundle.mainBundle().URLForResource("movie_quote", withExtension: "mp3")
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: filePath!, error: &error)
audioPlayer.enableRate = true
// handling the playbar limits
audioLengthSeconds = Float(nsTimerToSeconds(audioPlayer.duration))
audioPlayerBar.maximumValue = audioLengthSeconds
audioPlayerBar.minimumValue = 0.0
audioPlayerBar.value = 0.0
//setting time viewer
timeViewer.text = "0.0 / \(audioLengthSeconds)"
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func playRate(rate:Float) -> Void{
audioPlayer.rate = rate
audioPlayer.play()
// starting timer
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target:self, selector: Selector("updateSlider"), userInfo: nil, repeats: true)
}
func updateSlider(){
let currentTime = Float(nsTimerToSeconds(audioPlayer.currentTime))
audioPlayerBar.value = currentTime
timeViewer.text = "\(currentTime) / \(audioLengthSeconds)"
}
@IBAction func playSlowSound(sender: AnyObject) {
playRate(0.5)
}
@IBAction func playFastSound(sender: AnyObject) {
playRate(2.0)
}
@IBAction func stopPlayingAudio(sender: AnyObject) {
audioPlayer.stop()
}
@IBAction func SliderChange(sender: UISlider) {
audioPlayer.stop()
var newTime = audioPlayerBar.value
audioPlayer.currentTime = NSTimeInterval(newTime)
audioPlayer.play()
}
}