I have an app which plays a song, I have an external class which has a playSound func and all the variables needed:
class Song: NSObject {
var audioPlayer: AVAudioPlayer!
var deviceCurrentTime: NSTimeInterval!
func playSong() {
let sound = NSBundle.mainBundle().pathForResource("song", ofType: "mp3")
let soundData = NSData(contentsOfFile: sound!)
// self.audioPlayer = AVAudioPlayer(data: soundData!) throws
self.audioPlayer = nil
do {
self.audioPlayer = try AVAudioPlayer(data: soundData!)
}
catch {
print("Handle \(error) here")
}
let delay:NSTimeInterval = 0.0//100ms
var playtime:NSTimeInterval
playtime = audioPlayer.deviceCurrentTime + delay
self.audioPlayer?.playAtTime(playtime)
self.audioPlayer?.play()
}
and then in ViewDidLoad of my mainViewController i would like to play the song an acces the method from my Song object:
super.viewDidLoad()
let vc: NSObject = Song()
if let myVc = vc as? Song {
myVc.playSong()
}
but that doesnt play the song...if i put all the variables from Song to mainViewControler also with the playSong method and say playSong() in viewDidLoad, it works, but i would like to have it in external class.
Any help please? :)