Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

ios - Video stream not showing a view on iphone app

I have an ESP8266-CAM which I connect to my WiFi and stream it out. I have it so I can go onto any browser anywhere and type in the address http://xxx.xxxxx.xxxx.xx.xx/xxxxx/x and it will play. I have it working in a Blynk app also using this address. I would like to include it in an iPhone app I am building. I have the following code to play the stream.

#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

Even though when I press the button to showVideo and the log show all parts are activated, no screen shows up. Does anyone have any ideas why this is happening?

I have figured out that the camera is sending out a steam of mjpeg images that work in the video player in Blynk but apparently Apple doesn't use. I think this is the problem in that the player can't recognize or decode the format so no image nothing.
Does anyone have an idea of how to play an mjpeg image streams


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So I couldn't get the video to stream using AVPlayer, but by creating a WKWebView and opening the server in it I got the live video to play. Only thing is getting it to resize bigger one can see the details better. But at least it works. Here is the code.

#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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...