【问题标题】:Video stream not showing a view on iphone app视频流未在 iphone 应用程序上显示视图
【发布时间】:2021-04-07 22:28:48
【问题描述】:

我有一个 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 图像流

【问题讨论】:

  • 我已更改代码以了解状态。起初它说状态未知,然后它说它改变了,但它从来没有说要做什么。状态更改后,日志中不会打印出未知、准备播放或失败。它现在可以在XXXXXXXXX/xxxxxx/1 看到视频。帮助

标签: ios iphone video-streaming avkit


【解决方案1】:

所以我无法使用 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];

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多