iOS AVPlayer 播放/暂停按钮问题
我正在使用 AVPlayer
使用以下代码播放来自互联网的实时流:
NSString *u = @"http://192.192.192.192:8036";
NSURL *url = [NSURL URLWithString:u];
radiosound = [[AVPlayer alloc] initWithURL:url];
[radiosound play];
并且我有一个按钮播放:
[radiosound play];
和暂停:
[radiosound pause];
我的问题是我只想使用一个按钮播放/暂停,但是当我使用此代码时,
if (radiosound.isPlaying) {
[radiosound pause];
} else {
[radiosound play];
}
我的应用程序崩溃了,因为 AVPlayer 无法识别“isPlaying”。
有什么建议吗?
I am using AVPlayer
to play a live stream from the Internet using the following code :
NSString *u = @"http://192.192.192.192:8036";
NSURL *url = [NSURL URLWithString:u];
radiosound = [[AVPlayer alloc] initWithURL:url];
[radiosound play];
And I have one button play :
[radiosound play];
and Pause :
[radiosound pause];
My issue is that I want to use only one button Play/Pause, but when I am using this code
if (radiosound.isPlaying) {
[radiosound pause];
} else {
[radiosound play];
}
My app crashes, because AVPlayer doesn´t recognize "isPlaying".
Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AVPlayer
没有isPlaying
属性。使用rate
属性(0.0 表示停止,1.0 表示播放)。您可以查看
AVPlayer
类参考此处。AVPlayer
doesn't have anisPlaying
property. Use therate
property (0.0 means stopped, 1.0 playing).You can look at the
AVPlayer
class reference here.经过一番研究,我发现当没有网络连接时,
AVPlayer
在收到-play
消息后仍然将速率设置为1.0。因此,我还检查了 currentItem 并修改了我的方法,如下所示:
如果您认为这种方法有问题,请分享您的意见。
After some research I found out that when there is no network connection,
AVPlayer
still sets the rate to 1.0 after receiving a-play
message.Thus, I also check for the currentItem and modified my method like that:
Please share your opinion if you think something is wrong with this approach.