How to play video in iphone, xcode

I am creating a video application on iphone. I want to play the video in my application. My video is located locally. Please help me on how I can play the video in my application.

+3
source share
2 answers

Where is it located locally? In a set of applications? If so, the URL is as follows:

NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"videoname" ofType:@"mov" inDirectory:@""]];

You can play most videos using Apple MediaPlayer.

Add a framework (MediaPlayer) to your project and import it into a .hfile and create an instance of MediaPlayer, like this:

// .h:
#import <MediaPlayer/MediaPlayer.h>

// .m:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[player view] setFrame:[self.view bounds]]; // Frame must match parent view
[self.view addSubview:[player view]];
[player play];
[player release];

MPMoviePlayerController documentation

+8
source

:

- (void)embedYouTube {

    // If the url is like:
    //NSString *youtube = @"http://www.youtube.com/watch?v=EVdpzBT7Jrg";

    // Change to:
    NSString *videoURL = @"http://youtube.com/embed/EVdpzBT7Jrg";

    NSString *videoHTML = [NSString stringWithFormat:@"\
                 <html>\
                 <head>\
                 <style type=\"text/css\">\
                 iframe {position:absolute; top:50%%; margin-top:-130px;}\
                 body {background-color:#000; margin:0;}\
                 </style>\
                 </head>\
                 <body>\
                 <iframe width=\"100%%\" height=\"240px\" src=\"%@\" frameborder=\"0\" allowfullscreen></iframe>\
                 </body>\
                 </html>", videoURL];
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:self.view.frame];
    videoView.backgroundColor = [UIColor blackColor];
    videoView.opaque = NO;
    [self.view addSubview:videoView];
    [videoView loadHTMLString:videoHTML baseURL:nil];

}
0

Source: https://habr.com/ru/post/1766529/


All Articles