How to play a video from an URL in iphone

冷暖自知 提交于 2019-12-10 10:52:32

问题


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;  

}

回答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




回答2:


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.




回答3:


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.




回答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.



来源:https://stackoverflow.com/questions/5401531/how-to-play-a-video-from-an-url-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!