视频流未在 iphone 应用上显示视图

Video stream not showing a view on iphone app

我有一个 ESP8266-CAM,我连接到我的 WiFi 并将其流式传输出去。我有它,所以我可以在任何地方进入任何浏览器并输入地址 http://xxx.xxxxx.xxxx.xx.xx/xxxxx/x,它就会播放。我让它在 Blynk 应用程序中工作,也使用这个地址。我想将它包含在我正在构建的 iPhone 应用程序中。 我有以下代码来播放流。

#import "ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic,strong) AVPlayerItem * playerItem;
@property (nonatomic, strong )AVPlayerLayer *avPlayerLayer;

-(IBAction)Turnon:(id)sender;

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void) startplayer
   {
       NSURL *url = [[NSURL alloc]initWithString:@"xxxxxxxxxxx/xxxxx/1"];
       AVPlayerItem * playerItem= [AVPlayerItem playerItemWithURL:url];
       self.player = [AVPlayer playerWithPlayerItem:playerItem];
       self.player.allowsExternalPlayback= YES;
       self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
       self.avPlayerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
       //self.player.automaticallyWaitsToMinimizeStalling = false;
       self.avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
 //I HAVE TRIED THIS AT DIFFERENT LAYERS

       [self.view.layer insertSublayer:self.avPlayerLayer atIndex:0
        ];
//THIS DOES PRINT IN LOG
       NSLog(@"this is getting played");

      [self.player.currentItem addObserver:self
                                     forKeyPath:@"status"
                                        options:0
                                        context:nil];
}

-(IBAction)showVideo:(id)sender;{
    [self startplayer];
 }

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
   if (object == self.player.currentItem && [keyPath isEqualToString:@"status"])
   {
       NSLog(@"This is being called");
       [self.player play];
       self.player.rate = 1.0;
           if (self.player.currentItem.status == AVPlayerStatusReadyToPlay)
           {
//THIS IS ALSO BEING PRINTED IN LOG
               NSLog(@"This is being called");
                [self.player play];
                self.player.rate = 1.0;
            }
       }
   }
@end

即使当我按下按钮显示视频并且日志显示所有部分都已激活时,也没有屏幕出现。有谁知道为什么会这样吗?

我发现相机正在发送一系列 mjpeg 图像,这些图像在 Blynk 的视频播放器中有效,但显然 Apple 不使用。我认为这是播放器无法识别或解码格式的问题,所以没有图像。
有没有人知道如何播放 mjpeg 图像流

所以我无法使用 AVPlayer 播放视频,但通过创建 WKWebView 并在其中打开服务器,我可以播放实时视频。唯一的事情就是让它调整得更大,可以更好地看到细节。但至少它有效。这是代码。

#import <WebKit/WKWebViewConfiguration.h>

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
    
        self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(self.view.center.x-(320/2),self.view.center.y-(240/2), 320, 240) configuration:theConfiguration];
   
   
    NSURL *nsurl=[NSURL URLWithString:@"http://70.176.81.136:81/CFC878/1"];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [_webView loadRequest:nsrequest];
[self.view insertSubview:self.webView atIndex:14];