My audioPlayer is not working. I can't seem to understand why. I am a newbie and don't have much experience with swift. When I run, I am getting the println("audioPlayer not created") message. FilePath, FilePathUrl and mp3 have been confirmed and valid.
import UIKit import AVFoundation
class PlaySoundsViewController: UIViewController {
var audioPlayer:AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if let filePath = NSBundle.mainBundle().pathForResource("crankringtone", ofType: "mp3"){
var filePathUrl = NSURL.fileURLWithPath(filePath)
audioPlayer = AVAudioPlayer(contentsOfURL: filePathUrl, error: nil)
} else {
println("file path is empty")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func playSlowAudio(sender: UIButton) {
// here you can check if the player was successfully created
if var audioPlayer = audioPlayer {
// here you can check if your player is playing
if audioPlayer.playing {
//if playing lets pause it
audioPlayer.pause()
} else {
//if plaused lets play it
audioPlayer.play()
}
} else {
println("audioPlayer not created")
}
}
Aucun commentaire:
Enregistrer un commentaire