开发者

How to play a video from an URL in iphone

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] initWithF开发者_运维技巧rame: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;  

}


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜