如何在iphone, xcode中播放视频

时间:2022-08-27 00:10:33

i am creating a movie application in iphone . i want to play a video in my application. My Video url is located locally. Please anybody help me in how can i play a video in my application

我正在iphone上创建一个电影应用程序。我想在我的应用程序中播放一段视频。我的视频url位于本地。请大家帮助我如何在我的应用程序中播放视频

2 个解决方案

#1


8  

Where is it located locally? In the application bundle? If so, the URL is like this:

它位于哪里?在应用程序包吗?如果是,URL是这样的:

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

You can play most videos with Apple's MediaPlayer-framework.

你可以用苹果的mediaplayer框架播放大部分视频。

Add the framework (MediaPlayer) to your project and import it in the .h-file, and create an instance of the MediaPlayer like this:

将框架(MediaPlayer)添加到项目中,并导入到.h文件中,并创建MediaPlayer的实例,如下所示:

// .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

MPMoviePlayerController-documentation

#2


0  

THis code works:

这段代码:

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

}

#1


8  

Where is it located locally? In the application bundle? If so, the URL is like this:

它位于哪里?在应用程序包吗?如果是,URL是这样的:

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

You can play most videos with Apple's MediaPlayer-framework.

你可以用苹果的mediaplayer框架播放大部分视频。

Add the framework (MediaPlayer) to your project and import it in the .h-file, and create an instance of the MediaPlayer like this:

将框架(MediaPlayer)添加到项目中,并导入到.h文件中,并创建MediaPlayer的实例,如下所示:

// .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

MPMoviePlayerController-documentation

#2


0  

THis code works:

这段代码:

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

}