user2747220
user2747220

Reputation: 893

AVaudioPlayer returns nil

I don't know what to do anymore. The code below crashes on audioPlayer = AVAudioPlayer(contentsOfURL: newPath, error: &err)

This the URL of the file I am passing file:///var/mobile/Containers/Data/Application/1174C37F-CB78-4AB5-9C69-E1B906B48D97/Documents/Rocket_002.mp3

I have also tried passing the file through NSData but it doesn't work either.

import AVFoundation



class Player: NSObject, AVAudioPlayerDelegate {
var audioPlayer: AVAudioPlayer = AVAudioPlayer()


func playPodcastAt(path: String) {
    var err: NSError?
    let url = NSURL(string: path)
    if let newPath = url {
    println("attempting to play")
    audioPlayer = AVAudioPlayer(contentsOfURL: newPath, error: &err)
    if let error = err {
        println("audioPlayer error \(error.localizedDescription)")
    }
    println("Setting delegate")
    audioPlayer.delegate = self
    println("Playing")
    audioPlayer.prepareToPlay()
    audioPlayer.play()

        }
    }
}

This is the function in my ViewController which calls the class above

    @IBAction func playEpisode(sender: UIButton) {
    let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)

    let object = self.objectAtIndexPath(indexPath)
    if let result = object {
    if let isDownloaded = result["isDownloaded"] as? String {
        if isDownloaded == "yes" {
            if let url = result["localPath"] as? String {
                println("Attempting to play \(url)")
                 audioPlayer.playPodcastAt(url)
            }
        }
    }
  }
}

Edit: I have tried to save the file name in my database and retrieve the file in the main bundle using the code below. Still doesn't work:

 import AVFoundation



class Player: NSObject, AVAudioPlayerDelegate {
var audioPlayer: AVAudioPlayer = AVAudioPlayer()


func playPodcastAt(path: String) {
    println("path \(path)")
    let url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(path, ofType: "mp3")!)
    var err: NSError?
    audioPlayer = AVAudioPlayer(contentsOfURL: url, error: &err)
    if let error = err {
        println("audioPlayer error \(error.localizedDescription)")
    }
    println("Setting delegate")
    audioPlayer.delegate = self
    println("Playing")
    audioPlayer.prepareToPlay()
    audioPlayer.play()

        }
    }

Edit 2: Both responses below made me think of trying this approach, which worked:

 import AVFoundation



class Player: NSObject, AVAudioPlayerDelegate {
var audioPlayer: AVAudioPlayer = AVAudioPlayer()


func playPodcastAt(path: String) {
    println("path \(path)")
    if let directoryURL = NSFileManager.defaultManager()
        .URLsForDirectory(.DocumentDirectory,
            inDomains: .UserDomainMask)[0]
        as? NSURL {

    let newPath = directoryURL.URLByAppendingPathComponent(path)
    println(newPath)
    var err: NSError?
    audioPlayer = AVAudioPlayer(contentsOfURL: newPath, error: &err)
    if let error = err {
        println("audioPlayer error \(error.localizedDescription)")
    }
    println("Setting delegate")
    audioPlayer.delegate = self
    println("Playing")
    audioPlayer.prepareToPlay()
    audioPlayer.play()

        }
    }

}

Upvotes: 2

Views: 1599

Answers (2)

Norman G
Norman G

Reputation: 769

// Maybe try this for your Player Class. You will need to change the extension if your files are not m4a.

class Player: NSObject, AVAudioPlayerDelegate {

var audioPlayer: AVAudioPlayer = AVAudioPlayer()

func playPodcastAt(path: String) {

    if let soundUrl = NSBundle.mainBundle().URLForResource(path, withExtension:".m4a") {

        var err: NSError?
        audioPlayer = AVAudioPlayer(contentsOfURL: soundUrl, error: &err)
        if let error = err {
            println("audioPlayer error \(error.localizedDescription)")
        }
        audioPlayer.numberOfLoops = -1  // -1 runs forever, 0 runs once, 1 runs twice, etc.
        audioPlayer.volume = 1.0
        //audioPlayer.prepareToPlay()
        audioPlayer.play()

    } else {

        println("Path for file not found for audioPlayer.")
    }

}

}

Upvotes: 1

Leo Dabus
Leo Dabus

Reputation: 236260

The problem is that NSURL(string:) is for web links only. You have to use NSURL(fileURLWithPath:) for local resource files. Try like this:

if let url = NSURL(fileURLWithPath: path) {
    // you can use your local resource file url here
    var error:NSError?
    // you can also check if your local resource file is reachable
    if url.checkResourceIsReachableAndReturnError( &error ) {
         // url is reacheable
    } else if let error = error {
        println(error.description)
    }
}

Upvotes: 2

Related Questions