How to play a video from an URL in iphone

11,919

Solution 1

Just Try the following code

MPMoviePlayerController *theMoviPlayer;
NSURL *urlString=[NSURL URLWithString:@"http://www.boxmusiq.com/ThiruvasakamVideo/VTS_01_2_converted.mp4"];
theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:urlString];
theMoviPlayer.scalingMode = MPMovieScalingModeFill;
theMoviPlayer.view.frame = CGRectMake(0, 60, 320, 350);
[self.view addSubview:theMoviPlayer.view];
[self.theMoviPlayer play];

Note: Add Mediaplayer FrameKit and import it into your class

Solution 2

So, is it not playing the video, or anything else is happening. Please be more elaborative about the exact problem you are facing, when you post any question.

Can you print and post the log of the html(NSString) you are creating, and check whether it is correct.

Solution 3

In .h file

UIWebView *videoView;

In .m file

    videoView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 385)];

    [self embedYouTube :urlStr  frame:CGRectMake(0, 0, 320, 385)];

    [self.view addSubview:videoView];

    // methos to embed URL in HTML & load it to UIWebView

- (void)embedYouTube:(NSString*)url frame:(CGRect)frame

{

NSString* embedHTML = @”\

<html><head>\

<style type=\”text/css\”>\

body {\

background-color: transparent;\

color: white;\

}\

</style>\

</head><body style=\”margin:0\”>\

<embed id=\”yt\” src=\”%@\” type=\”application/x-shockwave-flash\” \

width=\”%0.0f\” height=\”%0.0f\”></embed>\

</body></html>”;



NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];

if(videoView == nil) {

videoView = [[UIWebView alloc] initWithFrame:frame];

[self.view addSubview:videoView];

}

[videoView loadHTMLString:html baseURL:nil];

}

**Note : The UIWebView for YouTube video doesn’t load when you run it on Simulator, work on device perfectly.

Solution 4

If you want to play a video from an url, you can use MPMoviePlayer instead of using UIWebView. Here is the link for example..

http://www.makebetterthings.com/blogs/iphone/play-video-on-iphone-and-ipad/

Hope it will help you.

Share:
11,919

Related videos on Youtube

Shanmugaraja G
Author by

Shanmugaraja G

Updated on June 04, 2022

Comments

  • Shanmugaraja G
    Shanmugaraja G almost 2 years

    I have a video at in url, In a button click I have to play a video, for this I used UIWebView to play this video, for this I used the following code:

    -(void)viewDidLoad
    {
       NSString *string = @"http://www.boxmusiq.com/ThiruvasakamVideo/VTS_01_2_converted.mp4";
       youtubeview = [[YouTubeView alloc] initWithStringAsURL:string frame:CGRectMake(0, 0, 320, 480)];
       NSLog(@"the url is: %@",string);
       [self.view addSubview:youtubeview];
    }
    

    and In YouTubeView.m YouTubeView is a subclass of UIWebView;

    - (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
    {
     if (self = [super init]) 
     {
                   // Create webview with requested frame size
       self = [[UIWebView alloc] initWithFrame:frame];
    
                   // HTML to embed YouTube video
       NSString *youTubeVideoHTML = @"<html><head>\
                             <body style=\"margin:0\">\
                             <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
                             width=\"%0.0f\" height=\"%0.0f\"></embed>\
                             </body></html>";
    
       // Populate HTML with the URL and requested frame size
       NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height];
    
       // Load the html into the webview
       [self loadHTMLString:html baseURL:nil];
           }
    
     return self;  
    
    }
    
  • jose920405
    jose920405 almost 9 years
    Not work for youtube videos. neither LBYouTubeView in ios 8.3