如何从iphone中的URL播放视频

时间:2011-03-23 06:19:40

标签: iphone objective-c iphone-sdk-3.0

我在网址上有一个视频,点按一下按钮我就要播放视频了,为此我用了UIWebView来播放这个视频,为此我使用了以下代码:

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

和在YouTubeView.m中 YouTubeView是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;  

}

4 个答案:

答案 0 :(得分:7)

试试以下代码

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

注意:添加Mediaplayer FrameKit并将其导入您的班级

答案 1 :(得分:1)

  

在.h文件中

UIWebView *videoView;
  

在.m文件中

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

}

* *注意:在Simulator上运行时,UIWebView for YouTube视频无法加载,无法正常使用设备。

答案 2 :(得分:0)

那么,是不是播放视频,还是其他任何事情都在发生。当您发布任何问题时,请更详细地说明您遇到的确切问题。

您可以打印并发布您正在创建的html(NSString)的日志,并检查它是否正确。

答案 3 :(得分:0)

如果要从网址播放视频,可以使用MPMoviePlayer而不是使用UIWebView。这是链接,例如..

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

希望它会对你有所帮助。