简体   繁体   English

Swift 隐藏视图 AVPlayerViewController

[英]Swift hide view AVPlayerViewController

I am wondering how do I hide the view of the AVPlayer.我想知道如何隐藏 AVPlayer 的视图。

guard let url = URL(string: "http://stream.radiomedia.com.au:8006/stream.m3u") else {
  return
}
// Create a new AVPlayer and associate it with the player view
let player = AVPlayer(url: url)

// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player

// Modally present the player and call the player's play() method when complete.
present(controller, animated: true) {
  player.play()
}

What I want to hide is the black screen it shows as this is just audio.我想隐藏的是它显示的黑屏,因为这只是音频。 No visual.没有视觉。

So I want to hide the viewer not necessarily the controlls.所以我想隐藏查看器不一定是控件。

You can use AVPlayer without AVPlayerViewController if you don't need the UI.如果您不需要 UI,则可以在没有AVPlayerViewController的情况下使用AVPlayer

You don't need AVPlayerViewController to play an audio.您不需要AVPlayerViewController来播放音频。 You can create a view controller of your own and use AVPlayer with AVPlayerLayer .您可以创建自己的视图 controller 并将AVPlayerAVPlayerLayer一起使用。

let player: AVPlayer = AVPlayer()
let playerLayer = AVPlayerLayer(player: self.player)
YOUR_VIEW.layer.addSublayer(self.playerLayer) 

// Play the audio
let audioURL = URL(string: url)
let playerItem = AVPlayerItem(url: audioURL)
player.replaceCurrentItem(with: playerItem)
player.play() 

You can hide YOUR_VIEW view if you'd like or display it if you are to play a video.如果您愿意,可以隐藏YOUR_VIEW视图,如果要播放视频,可以显示它。 It'd be better if player has a strong reference so I'd recommend storing it in a class variable.如果player有强参考会更好,所以我建议将其存储在 class 变量中。 Also call player.play() on AVPlayer.Status.readyToPlay event of AVPlayer .还要在AVPlayerAVPlayer.Status.readyToPlay事件上调用player.play() You can learn more onApple's documentation您可以在Apple 的文档中了解更多信息

You can update the background color of the player您可以更新播放器的背景颜色

let controller = AVPlayerViewController()
controller.contentOverlayView.backgroundColor = UIColor.white 

In case you don't want the UI like that of AVPlayerViewController , you can simply use AVPlayerLayer and add it as a view's sublayer .如果您不想要像AVPlayerViewController这样的 UI,您可以简单地使用AVPlayerLayer并将其添加为view's sublayer

guard let url = URL(string: "http://stream.radiomedia.com.au:8006/stream.m3u") else {
    return
}

let player = AVPlayer(url: url)

let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = view.bounds
playerLayer.videoGravity = .resizeAspect
playerLayer.contentsGravity = .resizeAspect
view.layer.insertSublayer(playerLayer, at: 0)

player.play()

AVPlayerLayer will play the video same as AVPlayerViewController . AVPlayerLayer将播放与AVPlayerViewController相同的视频。

Also, you can add the playerLayer to any view's layer.此外,您可以将playerLayer添加到任何视图的图层。 Be it main view or any custom view.无论是主视图还是任何自定义视图。

For more you can refer the link - https://medium.com/free-code-camp/how-to-set-up-video-streaming-in-your-app-with-avplayer-7dc21bb82f3有关更多信息,您可以参考链接 - https://medium.com/free-code-camp/how-to-set-up-video-streaming-in-your-app-with-avplayer-7dc21bb82f3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM