如何从URL流音频而不在设备上下载mp3文件 [英] How to stream audio from URL without downloading mp3 file on device

查看:73
本文介绍了如何从URL流音频而不在设备上下载mp3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Swift中从URL流音频而不在设备上下载mp3文件?我需要导入什么?我需要某些图书馆吗?将任何内容添加到info.plist吗?请注释您的代码.

How would I stream audio from a URL in Swift without downloading the mp3 file on the device? What do I need to import? Do I need certain libraries? Add anything to the info.plist? Please comment your code.

推荐答案

您可以使用iOS AVPLayer从网址流式传输音频.

You can use iOS AVPLayer for Streaming audio from url.

var player: AVPlayer!
let url  = URL.init(string:   "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")

let playerItem: AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)

let playerLayer = AVPlayerLayer(player: player!)

playerLayer?.frame = CGRect(x: 0, y: 0, width: 10, height: 50)
        self.view.layer.addSublayer(playerLayer!)
player.play()

这篇关于如何从URL流音频而不在设备上下载mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆