天道酬勤,学无止境

avplayeritem

为什么 AVAsset 状态变化的观察者不起作用?(Why does observer for status change of AVAsset not work?)

问题 我从 Apple 实现了以下代码。 它旨在观察 playerItem 状态的变化。 问题是由于某种原因它不起作用。 准备好后,observe 函数不会运行。 所有相关代码如下: func preloadVideo(media: Media) { //setup code, and then: media.playerItem1!.addObserver(self, forKeyPath: #keyPath(AVPlayerItem.status), options: [.old, .new], context: &playerItemContext) } 观察方法: private var playerItemContext = 0 override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { // Only handle observations for the playerItemContext print("jdslfhjkfdhaldfahjkflhajfldashkjfdshkjlas") guard context ==

2021-10-24 22:58:12    分类:技术分享    ios   swift   avfoundation   avplayeritem

An AVPlayerItem can occupy only one position in a player's queue at a time?

I have seen many questions on this and none have had a solution. I am getting: An AVPlayerItem can occupy only one position in a player's queue at a time In the console after quickly taping a view button which should have one of two states: Successfully preloaded video only a videoURL which will then be loaded in another VC. Why is this happening and how can I fix it? This is my code which handels this in the transitioned to VC: if numberMedia == 0 { if selectedPost!.interimMedia[numberMedia].playerLayer != nil { playCurrentMediaVid(currentMedia: selectedPost!.interimMedia[numberMedia]) } else

2021-10-22 01:23:55    分类:问答    ios   swift   avplayeritem   avqueueplayer

iOS - 使用 AVPlayer 检测 URL 流是否正常工作(iOS - Detecting if an URL stream is working or not, with AVPlayer)

问题 这是在我的代码中,从 url 播放的样子: private func play() { let streamUrl = ... let playerItem = AVPlayerItem(url: streamURL) radioPlayer = AVPlayer(playerItem: playerItem) radioPlayer.volume = 1.0 do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.mixWithOthers) try AVAudioSession.sharedInstance().setActive(true) UIApplication.shared.beginReceivingRemoteControlEvents() } catch { print("Error deactivating audio session.") } radioPlayer.play() startListeningForStreamFail() stopStartButton.setImage(#imageLiteral(resourceName: "pause_btn")

2021-10-21 17:05:04    分类:技术分享    ios   swift   notifications   avplayer   avplayeritem

AVPlayer 停止在线模式下播放 AES 加密的离线 HLS 视频(AVPlayer Stops Playing AES encrypted offline HLS Video in online mode)

问题 我编写了一个代码来下载 HLS 视频并在离线模式下播放。 此代码适用于编码视频。 现在我有一个 AES 加密的视频,我们有自定义的加密密钥。 下载 AES 加密的 HLS 视频后,我使用下面给定的代码来提供视频解密的密钥。 - (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest { NSString *scheme = loadingRequest.request.URL.scheme; if ([scheme isEqualToString:@"ckey"]) { NSString *request = loadingRequest.request.URL.host; NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:request]; if (data) { [loadingRequest.dataRequest respondWithData:data]; [loadingRequest finishLoading]; } else { //

2021-10-18 18:44:59    分类:技术分享    ios   aes   avplayer   http-live-streaming   avplayeritem

AVPlayer 状态 AVPlayerStatusFailed 无法正常工作(AVPlayer status AVPlayerStatusFailed not working Properly)

问题 我正在实现 AVPlayer(不是 AVAudioPlayer)并尝试从 URL 播放音频。 它工作正常。 但正如我们所知,有时我们可能会有无效的 URL(没有音频字节的 URL)。 当我从任何 URL 播放音频时,AVPlayer 状态总是显示 AVPlayerStatusReadyToPlay ,这是错误的。 例如:在 URL 部分,如果提供无效的 URL,如: http : //soundx.mp3slash.net/indian/jhankar_beats/1%28mp3pk.com%29.mp3(即使在您的浏览器中也不起作用) ,即使您提供https://stackoverflow.com/questions/ask或任何站点 URL(没有音频字节),AVPlayer 始终显示 AVPlayerStatusReadyToPlay。 我认为在这种情况下它的状态必须是 AVPlayerItemStatusUnknown 或 AVPlayerStatusFailed请帮助,如何知道 URL 无效(AVPlayerItemStatusUnknown 或 AVPlayerStatusFailed)表明我可以向用户显示消息。 提前致谢 -(void)playButtonClicked { NSURL *url = [NSURL URLWithString:AUDIO_URL]

2021-10-14 18:30:24    分类:技术分享    ios   objective-c   audio   avplayer   avplayeritem

AVPlayer status AVPlayerStatusFailed not working Properly

I am implementing AVPlayer (not AVAudioPlayer) and trying to play audio from URL. It is working fine. But as we know that sometimes we can have invalid URL (URL that does not have Audio byte). When I play audio from any URL AVPlayer status shows always AVPlayerStatusReadyToPlay which is wrong. For example : In URL part if give invalid URL like : http://soundx.mp3slash.net/indian/jhankar_beats/1%28mp3pk.com%29.mp3 (it does not works even in your browser) and even if you give https://stackoverflow.com/questions/ask or any site URL (which does not have audio bytes), AVPlayer shows always

2021-10-11 12:40:59    分类:问答    ios   objective-c   audio   avplayer   avplayeritem

即使在 player.status == .readyToPlay 之后 AVPlayerItem.duration 也会返回 NaN(AVPlayerItem.duration returns as NaN even after player.status == .readyToPlay)

问题 我有一个在 player.status 上使用 KVO 的实现,就像这里记录的建议一样。 这是相关的位: var player: AVPlayer? var url = URL(string: "some video url")! override func viewDidLoad() { super.viewDidLoad() initializeVideoPlayer() } func initializeVideoPlayer() { let playerItem = AVPlayerItem(url: url) self.player = AVPlayer(playerItem: playerItem) let playerLayer = AVPlayerLayer(player: player) playerLayer.frame = self.view.layer.bounds videoView.layer.addSublayer(playerLayer) player?.addObserver(self, forKeyPath: "status", options: .new, context: nil) } override func observeValue(forKeyPath keyPath: String?, of object: Any

2021-10-10 12:30:14    分类:技术分享    ios   swift   avplayer   key-value-observing   avplayeritem

AVPlayer 未在后台加载媒体(AVPlayer Not Loading Media In Background)

问题 在后台运行时,我的 AVPlayer 实现无法播放下载的音频(例如播客),但可以播放本地存储的歌曲。 只有当手机与我的电脑断开连接时才能在后台播放。 如果我的手机直接连接到我的计算机/调试器,则本地或下载的任何媒体都可以正常播放。 在前台,播放任何一种媒体类型也没有问题。 这是我的实现: AVPlayer *moviePlayer; AVPlayerItem *playerItem; NSURL *address = /* the url of either a local song or remote media */ if (moviePlayer != nil) { NSLog(@"removing rate, playbackBufferEmpty, playbackLikelyToKeepUp observers before creating new player"); [moviePlayer removeObserver:self forKeyPath:@"rate"]; [playerItem removeObserver:self forKeyPath:@"playbackBufferEmpty"]; [playerItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];

2021-10-09 22:47:25    分类:技术分享    ios   avplayer   background-process   avplayeritem

Why does observer for status change of AVAsset not work?

I implemented the following code from Apple. It is meant to observe for a change in the status of a playerItem. The problem is that for some reason it does not work. The observe function does not run when ready. All relevant code is below: func preloadVideo(media: Media) { //setup code, and then: media.playerItem1!.addObserver(self, forKeyPath: #keyPath(AVPlayerItem.status), options: [.old, .new], context: &playerItemContext) } Observe method: private var playerItemContext = 0 override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context

2021-10-06 18:25:27    分类:问答    ios   swift   avfoundation   avplayeritem

Swift AVPlayerItem 完成后关闭(Swift AVPlayerItem close when finished)

问题 我对 Swift 和 iOS 开发非常陌生,所以请原谅我的无知。 我正在尝试在视频播放完毕后自动关闭 AVPlayer。 我想附加“playerDidFinishPlaying”监听器来接收通知,但是一旦我有了它,我就找不到关闭控制器的方法/事件。 我想模仿点击“完成”按钮的动作。 这是一小段代码。 希望这是足够的信息。 如果没有,我可以提供更多信息 let destination = segue.destinationViewController as! AVPlayerViewController let url = NSURL(string: "video url") destination.player = AVPlayer(URL: url!) destination.player?.play() 我已经添加了以下通知,但同样,一旦我有了它,我不确定如何处理它...... NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: destination.player!.currentItem) func

2021-09-30 08:11:46    分类:技术分享    ios   swift   avplayer   avplayeritem