Simple Swift sound code(iOS)?

Hi everyone I am 15 years old boy who struggles with the ability to play sound in my iOS app...So here are some lines that I've already tried and everytime I build the app I just get some errors...


01. var audioPlayer = AVAudioPlayer()

02. let audioPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("myFile", ofType:
"mp3"))

03. audioPlayer = AVAudioPlayer(contentsOfURL: audioPath, error: nil)

04. audioPlayer.delegate = self

05. audioPlayer.prepareToPlay()

06. audioPlayer.play()


Xcode 7.0 beta(7A120f) says on line:

02. Value of optional type "String?" not unwrapped. (If I try to fix it I get another error witch has nothing in common with this line of code...And I am like WHAAAAT?)

04. Cannot assign a value.


Here's another code I've tried(simular to the first one):


var player : AVAudioPlayer! = nil //Initialization

@IBAction func playMyFile(sender: AnyObject?){
  let path = NSBundle.mainBundle().pathForResource("myFile", ofType:"mp3")
  let fileURL = NSURL(fileURLWithPath: path)
  player = AVAudioPlayer(contentsOfURL: fileURL, error: nil)
  player.prepareToPlay()
  player.delegate = self
  player.play()
}


I get the same errors on:

05. Value of optional type "String?" not unwrapped.

08. Cannot assign value.


Any ideas...I am really sorry if the question is really stupid but I tried everything and search the net, but nothing. Xcode gives me those mad errors and I decided to ask the PRO'S so...Thanks for the time and the help!!!

In your code, the return value for pathForResource is an optional, meaning that if the system cannot find that file it will return nil and make the rest of the code not work. You should wrap your code in "if let" statements that will allow the code to check for missing values before proceeding. So your code would look something like this:


var player : AVAudioPlayer! = nil

@IBAction func playMyFile(sender: AnyObject?) {
   if let path = NSBundle.mainBundle().pathForResource("myFile", ofType: "mp3") {
       if let fileURL = NSURL(fileURLWithPath: path) {
           player = AVAudioPlayer(contentsOfURL: fileURL, error: nil)
           player.prepareToPlay()
           player.delegate = self
           player.play()
       }
   }
}


As for the second error, make sure the class in this file is set as an AVAudioPlayerDelegate, then it should allow you to assign self to the delegate property of the player, like this (assuming your class is a view controller and is a subclass of UIViewController, adjust accordingly).


class MyViewController : UIViewController, AVAudioPlayerDelegate {
Accepted Answer

This code is taken from a sample code actually compiles and plays sound:

class SysSoundViewController: UIViewController, AVAudioPlayerDelegate {
    var soundFileURLRef: NSURL!

    var player: AVAudioPlayer?


    override func viewDidLoad() {
     
        super.viewDidLoad()
     
        let tapSound = NSBundle.mainBundle().URLForResource("tap", withExtension: "aif")
     
        self.soundFileURLRef = tapSound
     
        do {
            player = try AVAudioPlayer(contentsOfURL: soundFileURLRef)
        } catch _ {
            player = nil
        }
        player?.delegate = self
        player?.prepareToPlay()
    }

    @IBAction func playWithAVAudioPlayer(AnyObject) {
        NSLog("started playing")
        player?.play()
    }

    func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
        //
        NSLog("finished playing")
    }

}


By the way, new version of Xcode 7 beta is availale: Version 7.0 beta (7A121l), better use the newer version.

Thanks very MUCH THE BOOTH CODES WORK!!! THANKS AGAIN!🙂😁

Simple Swift sound code(iOS)?
 
 
Q