AVaudioPlayer返回nil

时间:2015-06-21 21:04:03

标签: swift avaudioplayer

我不知道该怎么办了。以下代码在System.Management.Instrumentation

上崩溃

这是我传递的文件的网址audioPlayer = AVAudioPlayer(contentsOfURL: newPath, error: &err)

我也试过通过NSData传递文件,但它也不起作用。

file:///var/mobile/Containers/Data/Application/1174C37F-CB78-4AB5-9C69-E1B906B48D97/Documents/Rocket_002.mp3

这是我的ViewController中调用上面的类

的函数
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()

        }
    }
}

编辑:我试图在我的数据库中保存文件名,并使用下面的代码检索主包中的文件。仍然不起作用:

    @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)
            }
        }
    }
  }
}

编辑2:下面的两个回复让我想到尝试这种方法,这有效:

 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()

        }
    }

}

2 个答案:

答案 0 :(得分:2)

问题是NSURL(string:)仅适用于网络链接。您必须使用NSURL(fileURLWithPath:)作为本地资源文件。试试这样:

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)
    }
}

答案 1 :(得分:1)

//也许为你的玩家类试试这个。如果您的文件不是m4a,则需要更改扩展名。

类播放器: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.")
    }

}

}