gpt4 book ai didi

iphone - rtsp://liveStream 与 A​​VPlayer

转载 作者:行者123 更新时间:2023-12-03 19:15:29 32 4
gpt4 key购买 nike

我想使用 AVPlayer 在 iPhone 设备上播放直播。另外我想从这个流中获取 CVPixelBufferRef 供下次使用。

我使用Apple guide用于创建播放器。目前,使用本地存储的视频文件,此播放器工作得很好,当我尝试播放此 AppleSampleStremURL - http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8 - 它的工作正弦也。

当我想使用 rtsp://播放流时出现问题,如下所示:rtsp://192.192.168.1:8227/TTLS/Streaming/channels/2?videoCodecType=H.264

代码 - 几乎全部使用 Apple 提供的 guid 完成,但无论如何:

准备播放资源

- (void)initialSetupWithURL:(NSURL *)url
{
NSDictionary *assetOptions = @{ AVURLAssetPreferPreciseDurationAndTimingKey : @YES,
AVURLAssetReferenceRestrictionsKey : @(AVAssetReferenceRestrictionForbidNone)};
self.urlAsset = [AVURLAsset URLAssetWithURL:url options:assetOptions];
}

准备播放器

- (void)prepareToPlay
{
NSArray *keys = @[@"tracks"];
__weak SPHVideoPlayer *weakSelf = self;
[weakSelf.urlAsset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf startLoading];
});
}];
}

- (void)startLoading
{
NSError *error;
AVKeyValueStatus status = [self.urlAsset statusOfValueForKey:@"tracks" error:&error];
if (status == AVKeyValueStatusLoaded) {
self.assetDuration = CMTimeGetSeconds(self.urlAsset.duration);
NSDictionary* videoOutputOptions = @{ (id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange)};
self.videoOutput = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:videoOutputOptions];
self.playerItem = [AVPlayerItem playerItemWithAsset: self.urlAsset];

[self.playerItem addObserver:self
forKeyPath:@"status"
options:NSKeyValueObservingOptionInitial
context:&ItemStatusContext];
[self.playerItem addObserver:self
forKeyPath:@"loadedTimeRanges"
options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld
context:&ItemStatusContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.playerItem];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didFailedToPlayToEnd)
name:AVPlayerItemFailedToPlayToEndTimeNotification
object:nil];

[self.playerItem addOutput:self.videoOutput];

self.assetPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];

[self addPeriodicalObserver];
NSLog(@"Player created");
} else {
NSLog(@"The asset's tracks were not loaded:\n%@", error.localizedDescription);
}
}

此处出现问题 - AVKeyValueStatus status = [self.urlAsset statusOfValueForKey:@"tracks"error:&error]; - 此行带有 rtsp://URL 返回 AVKeyValueStatusFailed

有错误:

Printing description of error:
Error Domain=AVFoundationErrorDomain Code=-11800
"The operation could not be completed"
UserInfo=0x7fd1ea5a8a10 {NSLocalizedFailureReason=An unknown error occurred (-12936),
NSLocalizedDescription=The operation could not be completed,
NSURL=rtsp://192.168.1.168:8556/PSIA/Streaming/channels/2?videoCodecType=H.264,
NSUnderlyingError=0x7fd1ea53f830 "The operation couldn’t be completed.
(OSStatus error -12936.)"}

我还寻找问题:

  1. FirstOne - 尝试使用 Apple 的此流示例应用程序 - StitchedStreamPlayer - 但我想在那里播放的流却出现了一些不同的错误
  2. SecondOne - 尝试使用这两个建议 - fileURLWithPath 返回不正确的 URL,例如:rtsp://192.168.1.168:8556/PSIA/Streaming/channels/2?videoCodecType=H.264 --file:/// - 所以我猜不正确
  3. 根据 AppleDev 的说法,尝试使用不同的方法创建 AvPlayer:如 [AVPlayer playerWithPlayerItem:playerItem][AVPlayer playerWithURL:url] - 没有任何改变。还可以尝试在 initialSetupWithURL 中为 AVAsset 设置不同的设置(请参阅上面的方法实现)。

那么,问题AVPlayer支持播放rtsp://流吗?如果是,有人可以提供正确用法的示例吗? Nad 我在代码中做错了什么?如果 AvPlayer 不支持 rtsp://可能存在一些替代解决方案?

最佳答案

您尝试过 MobileVLCKit 吗?这真的很简单而且效果很好!我写了一个small example here

如果您想尝试一下,只需在终端中输入 pod try ONVIFCamera 即可。

具体操作方法如下:

var mediaPlayer = VLCMediaPlayer()

// Associate the movieView to the VLC media player
mediaPlayer.drawable = self.movieView

let url = URL(string: "rtsp://IP_ADDRESS:PORT/params")
let media = VLCMedia(url: url)
mediaPlayer.media = media

mediaPlayer.play()

关于iphone - rtsp://liveStream 与 A​​VPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29371400/

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