Swift 4 Audio与Whatsapp共享

时间:2017-10-24 12:36:42

标签: ios swift whatsapp

我想通过WhatsApp分享音频(.mp3)文件。我使用UIActivityViewController,但它只是共享mp3文件链接而不是文件。我如何将文件共享给WhatsApp? 有我的代码:

 @IBAction func playTusu(_ sender: Any) {
    let url = URL(string: "https://freesound.org/data/previews/405/405511_2731495-lq.mp3")!
    let playerItem = CachingPlayerItem(url: url)

    playerItem.delegate = self

    player = AVPlayer(playerItem: playerItem)
    player.automaticallyWaitsToMinimizeStalling = false
    player.play()

}

@IBAction func paylas(_ sender: Any) {
    let url: [Any] = ["https://freesound.org/data/previews/405/405511_2731495-lq.mp3"]
    let avc = UIActivityViewController(activityItems: url, applicationActivities: nil)
    self.present(avc, animated: true)
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

感谢。

1 个答案:

答案 0 :(得分:1)

paylas功能中,您尝试共享String,但实际上应该是URL。这里也回答了这个问题:How do I share an Audio File in an App using Swift 3?

@IBAction func paylas(_ sender: Any) {
    let url: [Any] = [audioFileURL]
    let avc = UIActivityViewController(activityItems: url, applicationActivities: nil)
    self.present(avc, animated: true)
}

<强>更新

问题是你有一个文件,但你只有音频的URL。

正如@Leo Dabus在评论中提到的,你必须先下载音频并创建一个文件。看看这个问题Download mp3 file。您还必须等到音频下载完毕。然后,您可以共享该文件。确保在UI /主线程上执行此操作。