gpt4 book ai didi

iPhone HTTP 视频流 : How to show still image when playing audio-only stream

转载 作者:行者123 更新时间:2023-12-03 20:28:40 25 4
gpt4 key购买 nike

我有一个 iPad 应用程序使用变体播放列表播放 http 视频流,该列表具有针对不同带宽的不同比特率的多个流。其中之一是最低带宽的纯音频流。该应用程序使用AVPlayer来播放视频。由于某种原因,我无法使用 MPMoviePlayerViewController。

我遇到的问题是,当视频播放器播放纯音频流时,视频显示黑屏,但音频仍在播放。我认为这对用户来说很糟糕,他们不知道发生了什么。我想显示静态图像来代替视频播放器。像这样

Standard iPad Video Player - Playing Audio Stream only

有什么方法可以检测播放器何时切换到不同的流?或者检测流是否是纯音频?

最佳答案

由于我遇到了这个问题并且之前的答案不完整(不涵盖直播案例) - 这是我的改进:

if ([keyPath isEqual:@"tracks"])
{
BOOL hasVideoTrack = NO;
for (AVPlayerItemTrack* track in [[yourPlayer.currentItem] tracks])
{
if ([track.assetTrack.mediaType isEqual:AVMediaTypeVideo])
{
hasVideoTrack = YES;
break;
}
}
if (hasVideoTrack)
{
// Remove audio only view
} else {
// Show audio only view
}
}

请注意 - 但这只会让您显示本地仅音频屏幕。播放实时流时 - 仅音频的图稿应该来自流,因此我的代码更倾向于此:

if ([keyPath isEqualToString:@"timedMetadata"] == YES){
for (AVMetadataItem *metadata in self.player.currentItem.timedMetadata) {
if ([[metadata commonKey] isEqualToString:@"artwork"]) {
UIImage *overlayImage = [UIImage imageWithData:metadata.dataValue];
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:overlayImage];
overlayImageView.contentMode = UIViewContentModeScaleAspectFit;

// If an audio only slide is already there, make it disappear.
[self hideAudioOnlySlide];

self.audioOnlyView = overlayImageView;
[self showAudioOnlySlide];
self.audioOnlyView.size = _playerView.size;
break;
}
}


}else if ([keyPath isEqualToString:@"tracks"] == YES){
NSArray *tracks = self.player.currentItem.tracks;
if ([self.player.currentItem hasVideoTracks] == NO) {
// Check if there is timed metadata with artwork that indicates audio only is handled at the stream level.
BOOL hasAudioOnlyFromStream = NO;
for (AVMetadataItem *metadata in self.player.currentItem.timedMetadata) {
if ([[metadata commonKey] isEqualToString:@"artwork"]) {
hasAudioOnlyFromStream = YES;
break;
}
}
// If we don't have audio only slide from the stream - carry on to show audio only slide.
//Otherwise - this is handled by the timed metadata check for artwork.
if (hasAudioOnlyFromStream == NO) {
[self showAudioOnlySlide];
}
} else {
[self hideAudioOnlySlide];
}
}

添加观察代码:

[item addObserver:self forKeyPath:@"timedMetadata" options:0 context:NULL];
[item addObserver:self forKeyPath:@"tracks" options:0 context:NULL];

删除观察代码:

@try {
[item removeObserver:self forKeyPath:@"timedMetadata"];
[item removeObserver:self forKeyPath:@"tracks"];
}

hasVideoTracks 代码(在 AVPlayerItem 的类别内):

- (BOOL)hasVideoTracks{
BOOL hasVideoTracks = NO;
for (AVPlayerItemTrack* track in [self tracks]){
if ([track.assetTrack.mediaType isEqual:AVMediaTypeVideo]){
hasVideoTracks = YES;
break;
}
}

return hasVideoTracks;
}

注意:

  1. 不要使用presentationSize 仅检测音频 - 在 iOS 7 中,当仅检测音频时,它不再变成 CGSizeZero - 而是提供一些时髦的大小。这不是一个可靠的方法。
  2. 显示/隐藏audioOnlySlide 非常简单,应该包含您的实现。

关于iPhone HTTP 视频流 : How to show still image when playing audio-only stream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5887237/

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