Search code examples
iosobjective-cvideoios6video-streaming

how to play online video those are on server


I have videos on my server. I would like to play those video on iPhone and iPad.

Could anyone suggest how to deal with such case?

I know how to play video when the same video is in my project using MPMoviePlayerController.


Solution

  • You can use WebView to play video.

    In .h file add this code :

    #import <MediaPlayer/MediaPlayer.h>
        @interface VideoViewController : UIViewController<UIWebViewDelegate>
        @property (nonatomic, strong) UIWebView *playerView;
    

    And in .m file :

    @synthesize playerView;
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
             self.playerView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 05, 300, 200)];
            [self.view addSubview:self.playerView];
    
        }
        return self;
    }
    
    -(void)viewDidLoad
    {
        playerView.delegate = self;
        playerView.scrollView.scrollEnabled = NO;
        playerView.layer.cornerRadius = 5.0;
        playerView.clipsToBounds = YES;
    }
    

    And in playBtn action put this code :

    NSString *yourVideoLink = your video link;
    NSString *yourlinkThumbnail = your video thumbnaillink;
    [self playVideo: yourVideoLink withWebView:playerView andThumbnailLink: yourlinkThumbnail];
    
    
    #pragma mark - Webview Delegates
    
    - (void)webViewDidStartLoad:(UIWebView *)webView
    {
        //[SVProgressHUD showWithStatus:@"Loading..."];
    }
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        //[SVProgressHUD dismiss];
    }
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
    {
       // [SVProgressHUD dismiss];
    }
    
    #pragma mark - MoviePlayer Methods
    - (void)playVideo:(NSString *)urlString withWebView:(UIWebView*)videoView andThumbnailLink:(NSString*)thumbnailImageLink {
        NSString *embedHTML = @"\
        <html><head>\
        <style type=\"text/css\">\
        body {\
        background-color: transparent;\
        color: white;\
        }\
        </style>\
        <script>\
        function load(){document.getElementById(\"yt\").play();}\
        </script>\
        </head><body onload=\"load()\"style=\"margin:0\">\
        <video id=\"yt\" src=\"%@\" \
        width=\"%0.0f\" height=\"%0.0f\" poster=\"%@\" autoplay controls></video>\
        </body></html>";
        videoView.backgroundColor   =  [UIColor redColor];
        NSString *html = [NSString stringWithFormat:embedHTML, urlString, videoView.frame.size.width, videoView.frame.size.height,thumbnailImageLink];
        [videoView loadHTMLString:html baseURL:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlayFinished:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
    }
    
    -(void)videoPlayStarted:(NSNotification *)notification{
        //self.isInFullScreenMode = YES;
    }
    
    -(void)videoPlayFinished:(NSNotification *)notification{
        // your code here
       // self.isInFullScreenMode = NO;
    }