了解 AVPlayer 对象何时准备好播放
我正在尝试播放从先前的 UIView
传递到 UIView
的 MP3
文件(存储在 NSURL *fileURL 中)
变量)。
我正在初始化一个 AVPlayer
:
player = [AVPlayer playerWithURL:fileURL];
NSLog(@"Player created:%d",player.status);
NSLog
打印 Playercreated:0,
我认为这意味着它还没有准备好播放。
当我单击播放UIButton
时,我运行的代码是:
-(IBAction)playButtonClicked
{
NSLog(@"Clicked Play. MP3:%@",[fileURL absoluteString]);
if(([player status] == AVPlayerStatusReadyToPlay) && !isPlaying)
// if(!isPlaying)
{
[player play];
NSLog(@"Playing:%@ with %d",[fileURL absoluteString], player.status);
isPlaying = YES;
}
else if(isPlaying)
{
[player pause];
NSLog(@"Pausing:%@",[fileURL absoluteString]);
isPlaying = NO;
}
else {
NSLog(@"Error in player??");
}
}
当我运行此代码时,我总是在控制台中收到播放器错误?
。 但是,如果我用简单的 if(!isPlaying)
... 替换检查 AVPlayer
是否准备好播放的 if
条件,那么我第二次点击播放 UIButton
时,音乐开始播放。
控制台日志是:
Clicked Play. MP3:http://www.nimh.nih.gov/audio/neurogenesis.mp3
Playing:http://www.nimh.nih.gov/audio/neurogenesis.mp3 **with 0**
Clicked Play. MP3:http://www.nimh.nih.gov/audio/neurogenesis.mp3
Pausing:http://www.nimh.nih.gov/audio/neurogenesis.mp3
Clicked Play. MP3:http://www.nimh.nih.gov/audio/neurogenesis.mp3
2011-03-23 11:06:43.674 Podcasts[2050:207] Playing:http://www.nimh.nih.gov/audio/neurogenesis.mp3 **with 1**
我第二次看到,player.status
似乎保持为 1,我猜测是 AVPlayerReadyToPlay
。
我该怎么做才能让播放在我第一次单击播放 UIButton
时正常工作? (即,我如何确保 AVPlayer
不仅已创建,而且已准备好播放?)
I'm trying to play an MP3
file that is passed to an UIView
from a previous UIView
(stored in a NSURL *fileURL
variable).
I'm initializing an AVPlayer
with:
player = [AVPlayer playerWithURL:fileURL];
NSLog(@"Player created:%d",player.status);
The NSLog
prints Player created:0,
which i figured means it is not ready to play yet.
When i click the play UIButton
, the code i run is:
-(IBAction)playButtonClicked
{
NSLog(@"Clicked Play. MP3:%@",[fileURL absoluteString]);
if(([player status] == AVPlayerStatusReadyToPlay) && !isPlaying)
// if(!isPlaying)
{
[player play];
NSLog(@"Playing:%@ with %d",[fileURL absoluteString], player.status);
isPlaying = YES;
}
else if(isPlaying)
{
[player pause];
NSLog(@"Pausing:%@",[fileURL absoluteString]);
isPlaying = NO;
}
else {
NSLog(@"Error in player??");
}
}
When i run this, I always get Error in player??
in the console.
If i however replace the if
condition that checks if AVPlayer
is ready to play, with a simple if(!isPlaying)
..., then the music plays the SECOND TIME I click on the play UIButton
.
The console log is:
Clicked Play. MP3:http://www.nimh.nih.gov/audio/neurogenesis.mp3
Playing:http://www.nimh.nih.gov/audio/neurogenesis.mp3 **with 0**
Clicked Play. MP3:http://www.nimh.nih.gov/audio/neurogenesis.mp3
Pausing:http://www.nimh.nih.gov/audio/neurogenesis.mp3
Clicked Play. MP3:http://www.nimh.nih.gov/audio/neurogenesis.mp3
2011-03-23 11:06:43.674 Podcasts[2050:207] Playing:http://www.nimh.nih.gov/audio/neurogenesis.mp3 **with 1**
I see that the SECOND TIME, the player.status
seems to hold 1, which I'm guessing is AVPlayerReadyToPlay
.
What can I do to have the playing to work properly the first time i click the play UIButton
?
(ie, how can i make sure the AVPlayer
is not just created, but also ready to play?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您正在播放远程文件。
AVPlayer
可能需要一些时间来缓冲足够的数据并准备好播放文件(请参阅AV Foundation 编程指南)但是您似乎没有等到播放器准备好后再点击播放按钮。我想要的是禁用此按钮并仅在播放器准备好时才启用它。
使用KVO,可以收到玩家状态变化的通知:
当状态变化时将调用此方法:
You are playing a remote file. It may take some time for the
AVPlayer
to buffer enough data and be ready to play the file (see AV Foundation Programming Guide)But you don't seem to wait for the player to be ready before tapping the play button. What I would to is disable this button and enable it only when the player is ready.
Using KVO, it's possible to be notified for changes of the player status:
This method will be called when the status changes:
Swift 解决方案
您也可以通过这种方式使观察者无效
重要:您必须保留观察者变量,否则它将释放并且将不再调用changeHandler。因此,不要将观察者定义为函数变量,而是将其定义为实例变量,如给定的示例。
此键值观察器语法是 Swift 4 中的新语法。
有关更多信息,请参阅此处 https://github.com/ole/whats-new-in-swift-4/blob/master/Whats-new-in-Swift-4.playground/Pages/Key%20paths.xcplaygroundpage/Contents.swift
Swift Solution
Also you can invalidate the observer this way
Important: You must keep the observer variable retained otherwise it will deallocate and the changeHandler will no longer get called. So don't define the observer as a function variable but define it as a instance variable like the given example.
This key value observer syntax is new to Swift 4.
For more information, see here https://github.com/ole/whats-new-in-swift-4/blob/master/Whats-new-in-Swift-4.playground/Pages/Key%20paths.xcplaygroundpage/Contents.swift
我在尝试找出
AVPlayer
的状态时遇到了很多麻烦。status
属性似乎并不总是很有帮助,当我尝试处理音频会话中断时,这导致了无尽的挫败感。有时,AVPlayer
告诉我它已准备好播放(使用AVPlayerStatusReadyToPlay
),但实际上似乎并未准备好。我使用了 Jilouc 的 KVO 方法,但它并不是在所有情况下都有效。作为补充,当 status 属性没有用时,我通过查看
AVPlayer
的loadedTimeRanges
属性来查询 AVPlayer 已加载的流量。currentItem
(这是一个AVPlayerItem
)。这一切都有点令人困惑,但它看起来是这样的:
I had a lot of trouble trying to figure out the status of an
AVPlayer
. Thestatus
property didn't always seem to be terribly helpful, and this led to endless frustration when I was trying to handle audio session interruptions. Sometimes theAVPlayer
told me it was ready to play (withAVPlayerStatusReadyToPlay
) when it didn't actually seem to be. I used Jilouc's KVO method, but it didn't work in all cases.To supplement, when the status property wasn't being useful, I queried the amount of the stream that the AVPlayer had loaded by looking at the
loadedTimeRanges
property of theAVPlayer
'scurrentItem
(which is anAVPlayerItem
).It's all a little confusing, but here's what it looks like:
对于注册观察者
监听观察者
对于删除观察者
代码中的关键点是实例属性 isPlaybackLikelyToKeepUp。
For register observer
Listen the observer
For remove observer
The key point in the code is instance property isPlaybackLikelyToKeepUp.
经过大量研究并尝试多种方法后,我注意到通常
status
观察者并不能更好地了解AVPlayer
对象何时准备好播放,因为该对象可以准备播放,但这并不意味着它将立即播放。了解这一点的更好方法是使用
loadedTimeRanges
。After researching a lot and try many ways I've noticed that normally the
status
observer is not the better for know really whenAVPlayer
object is ready to play, because the object can be ready for play but this not that mean it will be play immediately.The better idea for know this is with
loadedTimeRanges
.基于 Tim Camber 回答,这是我使用的 Swift 函数:
或者,作为扩展
Based on Tim Camber answer, here is the Swift function I use :
Or, as an extension
我遇到了没有收到任何回调的问题。
事实证明,这取决于您创建流的方式。在我的例子中,我使用了一个playerItem来初始化,因此我必须将观察者添加到该项目中。
例如:
I had issues with not getting any callbacks.
Turns out it depends on how you create the stream. In my case I used a playerItem to initialize, and thus I had to add the observer to the item instead.
For example:
斯威夫特4:
Swift 4:
@JoshBernfeld 的答案对我不起作用。不知道为什么。他观察了
playerItem.observe(\.status
)。我必须观察player?.observe(\.currentItem?.status
。看起来它们是同一件事,playerItem status
属性设置playerStatusObserver = nil
当您使用完播放器后,
@JoshBernfeld's answer didn't work for me. Not sure why. He observed
playerItem.observe(\.status
. I had to observeplayer?.observe(\.currentItem?.status
. Seems like they're the same thing, theplayerItem status
property.when you are finished using the player set
playerStatusObserver = nil
检查玩家当前物品的状态:
Check the status of the player's currentItem: