来自网址

时间:2017-03-25 17:46:02

标签: ios swift xcode url mp3

我有一个简单的按钮,可以打开一个mp3的URL - 有没有办法在页面加载时自动播放mp3?否则,用户有两次基本上点击两次播放mp3(一次在应用程序中,再次在实际网页中)。我使用URL的原因是因为我想使用已经建立的音频播放器UI。谢谢!

@IBAction func openURL(_ sender: AnyObject) {
    if let url = NSURL(string: "http://marc.ucla.edu/mpeg/01_Breathing_Meditation.mp3"){ UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码 的目标c

NSURL *url = [NSURL URLWithString:@"http://marc.ucla.edu/mpeg/01_Breathing_Meditation.mp3"];
self.playerItem = [AVPlayerItem playerItemWithURL:url];
self.player = [AVPlayer playerWithPlayerItem:playerItem];
self.player = [AVPlayer playerWithURL:url];
[self.player play];

<强>夫特:

var url = URL(string: "http://marc.ucla.edu/mpeg/01_Breathing_Meditation.mp3")
playerItem = AVPlayerItem(url)
player = AVPlayer.withPlayerItem(playerItem)
player = AVPlayer.withURL(url)
player.play()