AVAudioPlayer中的url内容和返回nil的路径内容(Swift 2)

时间:2016-02-26 07:49:06

标签: ios swift avaudioplayer avplayer nsurl

    let url = NSURL(fileURLWithPath:receivedSongName)
    let urlstring = String(url)
    print("after url  \(urlstring)")
    do{
        player = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: receivedSongName),fileTypeHint: nil)
        player?.delegate = self
        player?.prepareToPlay()
    }
    catch{
        print ("Some error")
    }
    if player?.playing == false {
        player?.play()
    }

这是我的代码段。我收到了' receivedSongName'来自用户按下按钮的另一个类。只是为了调试,我将它强制为字符串并打印它。调试窗口中的输出类似:Haule%20Haule%20-%20www.123musiq.com%20-%20%20%C2%AE%20Riya%20collections%20%C2%AE - file:/// 。所以歌曲路径网址创建成功。但是当我尝试初始化播放器时,contentsOfUrl返回nil并且它将捕获块以进行打印:出现一些错误。我已经导入了AVFoundation并且还使用了AVAudioPlayerDelegate协议。这个问题仍然存在。我还尝试将数据转换为NSData并从那里播放文件。这是代码片段。

    let url = NSURL(fileURLWithPath:receivedSongName)
    let data : NSData = NSFileManager.defaultManager().contentsAtPath(String(url))!
   let urlstring = String(url)
    print("after url  \(urlstring)")
    do{
    player = try AVAudioPlayer(data: data)
    player?.delegate = self
    player?.prepareToPlay()
    }
    catch{
        print ("Some error")
    }

这里,数据无法初始化,因为contentsAtPath返回nil,因此在展开可选项时找到nil。因此,卡在修复中。任何帮助都将非常感激。

0 个答案:

没有答案