我正在实现 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];
[self.audioSliderBar setMinimumValue:AUDIO_SLIDER_BAR_0];
playerItem = [AVPlayerItem playerItemWithURL:url];
player = [AVPlayer playerWithPlayerItem:playerItem];
player.volume=5.0;
[player addObserver:self forKeyPath:STATUS options:0 context:nil];
[playerItem addObserver:self forKeyPath:PLAY_BACK_BUFFER_EMPTY options:NSKeyValueObservingOptionNew context:nil];
[playerItem addObserver:self forKeyPath:PLAY_BACK_LIKELY_TO_KEEP_UP options:NSKeyValueObservingOptionNew context:nil];
addObserverFlag=YES;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change: (NSDictionary *)change context:(void *)context
{
if (object == player && [keyPath isEqualToString:STATUS])
{
if (player.status == AVPlayerStatusFailed)
{
[ViewUtilities showAlert:AUDIO_PLAYER :AUDIO_PLAYER_FAILED_MESSAGE];
}
else if (player.status == AVPlayerStatusReadyToPlay)
{
else if (player.status == AVPlayerItemStatusUnknown)
{
[ViewUtilities showAlert:AUDIO_PLAYER :AUDIO_PLAYER_UNKNOWN];
}
if (!player)
{
return;
}
}
}`
最佳答案
Possible values of the status property, to indicate whether it can successfully play items.
AVPlayerStatusReadyToPlay Indicates that the player is ready to play AVPlayerItem instances.
所以我认为您需要通过 AVPlayerItemStatus 类型的 status 属性检查特定项目的状态
关于ios - AVPlayer 状态 AVPlayerStatusFailed 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32457562/