首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVaudioPlayer返回零

AVaudioPlayer返回零
EN

Stack Overflow用户
提问于 2015-06-21 21:04:03
回答 2查看 1.5K关注 0票数 2

我不知道该怎么办了。下面的代码在audioPlayer = AVAudioPlayer(contentsOfURL: newPath, error: &err)上崩溃

这是我传递给file:///var/mobile/Containers/Data/Application/1174C37F-CB78-4AB5-9C69-E1B906B48D97/Documents/Rocket_002.mp3的文件的URL

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

代码语言:javascript
复制
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中的函数,它调用上面的类

代码语言:javascript
复制
    @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)
            }
        }
    }
  }
}

编辑:我尝试将文件名保存在数据库中,并使用下面的代码在主包中检索文件。还是没有用:

代码语言:javascript
复制
 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:下面的两种反应让我想到了尝试这种方法,它起了作用:

代码语言:javascript
复制
 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()

        }
    }

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-21 21:46:47

//也许可以为您的玩家类尝试此方法。如果文件不是m4a,则需要更改扩展名。

类玩家: NSObject,AVAudioPlayerDelegate {

代码语言:javascript
复制
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.")
    }

}

}

票数 1
EN

Stack Overflow用户

发布于 2015-06-21 21:17:02

问题是,NSURL(string:)只适用于网络链接。您必须对本地资源文件使用NSURL(fileURLWithPath:)。就像这样:

代码语言:javascript
复制
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)
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30969311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档