我不知道该怎么办了。下面的代码在audioPlayer = AVAudioPlayer(contentsOfURL: newPath, error: &err)上崩溃
这是我传递给file:///var/mobile/Containers/Data/Application/1174C37F-CB78-4AB5-9C69-E1B906B48D97/Documents/Rocket_002.mp3的文件的URL
我也尝试过通过NSData传递文件,但是它也不起作用。
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()
}
}
}这是我的ViewController中的函数,它调用上面的类
@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)
}
}
}
}
}编辑:我尝试将文件名保存在数据库中,并使用下面的代码在主包中检索文件。还是没有用:
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:下面的两种反应让我想到了尝试这种方法,它起了作用:
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()
}
}}
发布于 2015-06-21 21:46:47
//也许可以为您的玩家类尝试此方法。如果文件不是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.")
}
}}
发布于 2015-06-21 21:17:02
问题是,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)
}
}https://stackoverflow.com/questions/30969311
复制相似问题