gpt4 book ai didi

ios - AVPlayer Observer 从不调用无效的 URL

转载 作者:技术小花猫 更新时间:2023-10-29 11:20:26 47 4
gpt4 key购买 nike

我正在尝试做一个基本的广播应用程序,我得到了一个 URL 列表:当我尝试调用一个无效的 URL(错误的路径或没有可播放文件的正确路径)时,Observer 似乎从未被调用过。这是我的部分代码:

        urlStream = [NSURL URLWithString:mp3URL];  

self.playerItem = [AVPlayerItem playerItemWithURL:urlStream];

[playerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
[appDelegate.player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];

[appDelegate.player replaceCurrentItemWithPlayerItem:playerItem]; //OK

[appDelegate.player play];

在相对观察者方法中我得到:

    if (object == playerItem && [keyPath isEqualToString:@"playbackBufferEmpty"])
{
if (playerItem.playbackBufferEmpty)
{
NSLog(@"playbackBufferEmpty");
[self alertBuffer];
}

}

if (object == playerItem && [keyPath isEqualToString:@"playbackLikelyToKeepUp"])
{
if (playerItem.playbackLikelyToKeepUp)
{
NSLog(@"playbackLikelyToKeepUp");
[loadStation stopAnimating];

if (playerItem.status == AVPlayerItemStatusReadyToPlay) {
NSLog(@"AVPlayerItemStatusReadyToPlay");
}
else if (playerItem.status == AVPlayerStatusFailed) {
NSLog(@"AVPlayerStatusFailed");
}
else if (playerItem.status == AVPlayerStatusUnknown) {
NSLog(@"AVPlayerStatusUnknown");
}
}
}

else if (object == appDelegate.player && [keyPath isEqualToString:@"status"]) {
if (appDelegate.player.status == AVPlayerStatusReadyToPlay) {
NSLog(@"Player Status = Ready to Play");

}
if (appDelegate.player.status == AVPlayerStatusUnknown) {
NSLog(@"Player Status = Sometimes did wrong.");
[self alertUnknown];
}
if (appDelegate.player.status == AVPlayerStatusFailed) {
NSLog(@"Player Status = Status Failed.");
[self alertStatusFailed];
}

}

无论我调用什么 URL,我只会得到 ReadyToPlay 状态:当我选择无效的 URL 时,什么也不会发生。一个 URL 不工作的例子是:http://audioplayer.wunderground.com:80/RHBrant/Cheyenne.mp3.m3u

我哪里错了?

非常感谢。

最佳答案

好吧,我发现了我的错误:我将观察者添加到 appDelegate.player 以捕获错误的 URL,而我所要做的就是添加到 playerItem:

        [playerItem addObserver:self forKeyPath:@"status" options:0 context:nil];

在相关方法中:

         if ([playerItem status] == AVPlayerStatusFailed) {
NSLog(@"playerItem Status = Failed.");
NSLog(@"Error = %@",error.description);
return;
}

现在好了。

谢谢

关于ios - AVPlayer Observer 从不调用无效的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9785476/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com