【问题标题】:How to play downloaded audio file swift如何快速播放下载的音频文件
【发布时间】:2017-04-02 15:19:39
【问题描述】:

我有 2 个音频文件变体,第一个是嵌入的,第二个是我从服务器下载的。
第一个瓦里安工作得很好。 但是当我从服务器获取音频文件时没有播放并出现错误。

我得到路径:

file:///var/mobile/Containers/Data/Application/24656EA9-6F1F-4C0F-A6FE-5D457908A80D/Documents/Dumont.mp3

并尝试玩

func playMusic(filename: String) {
    let url = NSBundle.mainBundle().URLForResource(filename, withExtension: "mp3")
    guard let newURL = url else {
        print("Could not find file: \(filename)")
        return
    }
    do {
        player = try AVAudioPlayer(contentsOfURL: newURL)
        player!.numberOfLoops = -1
        player!.prepareToPlay()
        player!.play()
    } catch let error as NSError {
        print(error.description)
    }
}

如何在swift 2.3中播放这个音频文件?

平台 iOS >8., Swift 2.3

【问题讨论】:

    标签: ios swift audio avaudioplayer


    【解决方案1】:

    嘿,为你投反对票的愚蠢家伙:

    if let dir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
      let path = NSURL(fileURLWithPath: dir).URLByAppendingPathComponent("Dumont.mp3")
        do {
            player = try AVAudioPlayer(contentsOfURL: path)
            player!.numberOfLoops = -1
            player!.prepareToPlay()
            player!.play()
          } catch let error as NSError {
             print(error.description)
          }
    

    此代码播放下载的歌曲!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      相关资源
      最近更新 更多