开发者

How to play YouTube Movie on an iPhone Application when tapped on UITableViewCell?

I'm a newbie to iPhone development and just started on developing an application that contains a UITableView where each cell consisting of youtube video thumbnails in the form of webviews. For embedding YouTube Player on iPhone, I have used the follwing piece of code.

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

}

Now that I can see the thumbnail on the tableviewcell and once I tap on the thumbnail, YouTube player opens and play the movie.

My thumbnail occupies only a small portion of the cell and the rest of the area of the cell contains some text descriptions. My problem is that I must tap exactly on the thumbnail for the movie to play. If I tap somewhere else on the cell then it wouldn't play because my thumbnail doesn't extend all over the cell.

Isn't there a way to make the movie to play in didSelectRowAtIndexPath? I have seen some chaps suggest using Javascript but nobody seem to have an idea on the correct way of using it for this problem.

Highly appreciate it if anybody can help.


I suggest you to take a look at YouTube APIs and try to figure it out what is your real problem. but another good link is How To Play YouTube Videos Within an Application. Hope I'm helping you.


It's maybe not the best solution but It works:

//get your UIWebview in tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//...

videoView.mediaPlaybackRequiresUserAction = NO;
[videoView loadHTMLString:[self generateHtmlEmbedYouTube] baseURL:nil];

From the Documentation

mediaPlaybackRequiresUserAction:

A Boolean value that determines whether HTML5 videos can play automatically or require the user to start playing them.

The onely problem with this solution is that you have to reload your UIWebView.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜