gpt4 book ai didi

swift - 使用 AVPlayer 的直播无法在 iOS 11 中播放

转载 作者:行者123 更新时间:2023-11-28 06:11:46 28 4
gpt4 key购买 nike

我正在尝试从远程 url 流式传输音乐。我正在尝试在 iOS 11 中运行它,但它无法播放音乐。

View Controller

var session = AVAudioSession.sharedInstance()

var LQPlayer: AVPlayer?
let LOW_URL = URL(string: "http://someLInk.pls")! // not an original url provided at this time.

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.avPlayerSetup()
}

func avPlayerSetup() {
do {
try session.setCategory(AVAudioSessionCategoryPlayback)
try session.overrideOutputAudioPort(.none)
try session.setActive(true)
} catch {
print("AVPlayer setup error \(error.localizedDescription)")
}
}

func initPlayer() {
LQPlayer = AVPlayer(url: LOW_URL)
print("player allocated")
}

func deAllocPlayer() {
LQPlayer = nil
print("player deallocated")
}

@IBAction func playBtn(_ sender: Any) {
initPlayer()
LQPlayer?.play()
}

@IBAction func pauseBtn(_ sender: Any) {
LQPlayer?.pause()
deAllocPlayer()
}
}

我在 info.plist 中设置了 Allow Arbitrary Loads YES。
上面的代码我给出的 URL 是虚拟的。实际网址工作正常。

最佳答案

使用实时视频流的工作代码

    @IBOutlet weak var player_View: UIView!
var LQPlayer: AVPlayer?
let LOW_URL = URL(string:"http://www.streambox.fr/playlists/test_001/stream.m3u8")!


override func viewDidLoad() {
super.viewDidLoad()

self.avPlayerSetup()

LQPlayer = AVPlayer.init(url: LOW_URL)
let avPlayerView = AVPlayerViewController()
avPlayerView.view.frame = self.player_View.bounds
avPlayerView.player = LQPlayer

self.player_View.addSubview(avPlayerView.view)

}

func avPlayerSetup() {

do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
try audioSession.setActive(true)
} catch {
print("AVPlayer setup error \(error.localizedDescription)")
}

}

func initPlayer() {
LQPlayer = AVPlayer(url:LOW_URL)
print("player allocated")
}

func deAllocPlayer() {
LQPlayer = nil
print("player deallocated")
}

@IBAction func playBtn(_ sender: Any) {
// initPlayer()
LQPlayer?.play()
}

@IBAction func pauseBtn(_ sender: Any) {
LQPlayer?.pause()
deAllocPlayer()
}

关于swift - 使用 AVPlayer 的直播无法在 iOS 11 中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46338775/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com