1

I'm building a tableview in which each cell represents a sound that is played when the user taps that particular cell.

In objective-C it worked fine, but now that Apple released Swift I decided to move over instantly, but for some reason the sound does not play. My code when the user taps the cell:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    var currentItem = self.items[indexPath.row]

    var audioPath = NSString(string: NSBundle.mainBundle().pathForResource(currentItem.soundID, ofType: "mp3"))

    println(audioPath)

    var audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(string: audioPath), error: nil)
    audioPlayer.play()
}

currentItem is the object in the array that has to be called. Each sound I can play is put in a custom object, together with a title and an image. that object is put in an instance of currentItem.

This is what the printNL outputs when I tapp one of my cells:

/private/var/mobile/Containers/Bundle/Application/ADF0CAFC-4C9E-475E-B3F0-CD85A1873CA5/Juichen.app/StupidQuestion.mp3

it does not give an error. I already tried moving the sound file to other folders, but that does not solve the problem either. therefore, I assume that this problem occurs because I am calling the audioPlayer incorrect?

Any help would be highly appreciated!

Joris416
  • 4,751
  • 5
  • 32
  • 59
  • The only thing I can see between your code and another example is the other example calls `audioPlayer.prepareToPlay()`. Can you try that? – Putz1103 Jun 24 '14 at 12:49
  • Sorry to say but that's not solving the problem. Thanks for the reply though. Currently, my audio (.mp3) files are located in Xcode in the folder `/Art/Sounds`, and in the project folder `SoundList/Soundlist` (as the name of my project is, guess what, Soundlist.). I already tried to move the files to other folders but that was not solving the problem. maybe I have to edit the file path? If so, How do I do that? – Joris416 Jun 24 '14 at 12:58
  • The way you are getting the path of the mp3 is such that if the mp3 did not exist the path would be `nil`. Since you are getting a valid path from that function the file is there. That is not your issue. It's possible that your URL is not correct for some reason. Make sure it is good and valid. – Putz1103 Jun 24 '14 at 13:01

1 Answers1

2

Let say you have a class myTable:

class myTable : UITableViewController
{
    var audioPlayer:AVAudioPlayer? = nil

...
}

And to initialize audioPlayer:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) 
{
        var currentItem = self.items[indexPath.row]

        var audioPath = NSString(string: NSBundle.mainBundle().pathForResource(currentItem.soundID, ofType: "mp3"))

        println(audioPath)

        var error : NSError? = nil
        self.audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(string: audioPath), error: &error)

        if (self.audioPlayer == nil)
        {
            if let playerError = error as? NSError
            {
                let des : String? = playerError.localizedDescription
                println("Error: \(des)")
            }
        }
        else
        {
            self.audioPlayer.play()
        }
}

Hope this helps.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • Hi, thanks for your reaction! If I implement this code I get 2 errors on the NSError line: `Excluded module member name after module name` and `NSError is not a subtype of 'module'`. How would I solve this? – Joris416 Jun 24 '14 at 13:30
  • @Imaginedigital Ok, I missed that you're passing `nil` in `error`. Please see update in a sec. – Unheilig Jun 24 '14 at 13:37
  • 1
    Thanks, sound is playing flawless now! – Joris416 Jun 25 '14 at 10:55
  • 1
    Hi. Ive been looking for a simple way to put sound in my Swift program and you guys seem to know what you're talking about. I tried the code above in my program and it doesn't recognize AVAudioPlayer. Would someone help me out here? @Unheilig – blue Sep 20 '14 at 23:58
  • 1
    @Unheilig You'll have to add AVFoundation.framework to your application your linked frameworks and libraries and import AVFramework to the top of any file using AVAudioPlayer. – jamesnotjim Oct 14 '14 at 04:41