Moh*_*hit 2 xcode ios avplayer appdelegate swift
选择collectionViewCell时,我试图加载AVPlayer,这是我的代码didSelectItem:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let item = items?[indexPath.item] {
showPlayer(item: item)
}
}
func showPlayer(item: Item) {
let playerLauncher = PlayerLauncher()
playerLauncher.showVideoPlayer()
let playerView = PlayerView()
playerView.item = item
playerView.setupPlayerView()
}
Run Code Online (Sandbox Code Playgroud)
这是我的PlayerLauncher文件:
class PlayerView: UIView {
var item: Item? {
didSet {
if let id = item?.itemId {
itemId = id
}
}
}
var itemId = String()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor(white: 0.3, alpha: 1)
}
var player: AVPlayer?
func setupPlayerView() {
let baseurl = "http://xample.com/play.m3u?id="
let urlString = "\(baseurl)\(itemId)"
print(urlString)
if let url = URL(string: urlString) {
player = AVPlayer(url: url)
let playerLayer = AVPlayerLayer(player: player)
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.frame
let audioSession = AVAudioSession.sharedInstance()
do{
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
} catch let err {
print(err)
return
}
player?.play()
player?.addObserver(self, forKeyPath: "currentItem.status", options: .new, context: nil)
}
}
}
class PlayerLauncher: NSObject {
func showVideoPlayer() {
print("Showing the player...")
if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.backgroundColor = UIColor.white
view.frame = CGRect(x: keyWindow.frame.width - 10, y: keyWindow.frame.height - 10, width: 10, height: 10)
let playerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.height)
let playerView = PlayerView(frame: playerFrame)
view.addSubview(playerView)
keyWindow.addSubview(view)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
view.frame = keyWindow.frame
}, completion: { (completedAnimation) in
//later...
})
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,每当我选择该项目时,播放器便会开始加载,控制台会打印URL(因为我已经在其中添加了print语句),这就是它的输出:
http://xample.com/play.m3u?id=12345
(lldb)
Run Code Online (Sandbox Code Playgroud)
然后崩溃,并Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)在AppDelegate中显示错误。我该如何解决?
谢谢。
更新:
我didSelectItem稍微改变了功能。这是代码:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let item = items?[indexPath.item] {
showPlayer(item: item)
}
}
func showPlayer(item: Item) {
let playerLauncher = PlayerLauncher()
playerLauncher.showPlayer(item: item)
}
Run Code Online (Sandbox Code Playgroud)
我将init方法更改PlayerView为此:
var item: Item? {
didSet {
if let id = item?.itemId {
itemId = id
}
}
}
init(frame: CGRect, item: Item) {
super.init(frame: frame)
self.item = item
setupPlayerView()
setupContainerView()
backgroundColor = UIColor(white: 0.3, alpha: 1)
}
Run Code Online (Sandbox Code Playgroud)
以及所需的更改PlayerLauncher:
let playerView = PlayerView(frame: playerFrame, item: item)
view.addSubview(playerView)
Run Code Online (Sandbox Code Playgroud)
我在let playerView网上休息了一下,可以看到Id传递到那里。但是它不会传递给PlayerView类中的urlString 。因此,ID仍为" "。
我制作了一份指南,希望能说明您的误解。
对于未使用MARKDOWN文本格式的其他读者,我深表歉意,但请确保这是我对原始海报的最大尝试。在上一篇文章中,我们已经进行了充分的讨论。
那么,您如何解决呢?我想直接给您提供代码没有什么好处,请尝试根据此信息修复代码。
如果您还有其他问题,我会尽力为您提供帮助:)
| 归档时间: |
|
| 查看次数: |
3473 次 |
| 最近记录: |