5

我试图在不触摸屏幕的情况下与 UIWebView 中加载的页面进行交互。我试过 mediaplaybackrequiresuseraction 没有成功。我也尝试过这里提供的方法,但显然它在 ios 6 中不起作用。我正在尝试加载url 并让播放器开始播放。有谁知道如何使这项工作 ios 6?任何帮助是极大的赞赏。

我的代码

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


  int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

  NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];
  //storyLink is various form of http://link.brightcove.com/services/player/bcpid1683318714001?bckey=AQ~~,AAAAC59qSJk~,vyxcsD3OtBPHZ2UIrFX2-wdCLTYNyMNn&bclid=1644543007001&bctid=2238216269001 with different bctid=value

  NSURL *kitcoURL = [NSURL URLWithString:storyLink];
  NSURLRequest *requestURL = [NSURLRequest requestWithURL:kitcoURL];
  [videoScreen setUserInteractionEnabled:YES];
  [videoScreen setMediaPlaybackRequiresUserAction:NO];
  [videoScreen loadRequest:requestURL];

 }

//方法在ios6中不起作用

 - (UIButton *)findButtonInView:(UIView *)view {
UIButton *button = nil;

if ([view isMemberOfClass:[UIButton class]]) {
return (UIButton *)view;
}

if (view.subviews && [view.subviews count] > 0) {
for (UIView *subview in view.subviews) {
  button = [self findButtonInView:subview];
  if (button) return button;
}
}

return button;
}

- (void)webViewDidFinishLoad:(UIWebView *)_webView {
  UIButton *b = [self findButtonInView:_webView];
  [b sendActionsForControlEvents:UIControlEventTouchUpInside];
 }

编辑 //videoScreen 是我的 UIWebView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *urlString = @"http://link.brightcove.com/services/player/bcpid1683318714001?bckey=AQ~~,AAAAC59qSJk~,vyxcsD3OtBPHZ2UIrFX2-wdCLTYNyMNn&bclid=1644543007001&bctid=2238216269001";

NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"bckey=([^&]*)" options:0 error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, urlString.length)];
NSString *bckey = [urlString substringWithRange:[match rangeAtIndex:1]];

regex = [NSRegularExpression regularExpressionWithPattern:@"bctid=([0-9]*)" options:0 error:&error];
match = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, urlString.length)];
NSString *bctid = [urlString substringWithRange:[match rangeAtIndex:1]];

NSString *newUrlString = [NSString stringWithFormat:@"http://c.brightcove.com/services/viewer/htmlFederated?&playerKey=%@&videoID=%@", bckey, bctid];

[videoScreen loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:newUrlString]]];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (!webView.isLoading)
{
    [self checkForVideoReady];
}

}
 // Checking player object for ready. If it ready - starting video. Otherwise waiting 1 second 
- (void)checkForVideoReady
{
    if (![[videoScreen stringByEvaluatingJavaScriptFromString:@"player.ready"]   isEqualToString:@"true"])
    {
    [self performSelector:@selector(checkForVideoReady) withObject:nil afterDelay:1];
   }
   else
   {
        [videoScreen stringByEvaluatingJavaScriptFromString:@"player.videoPlayer.playButton.element.click();"];
   }
 }
4

4 回答 4

4

如果你只想播放一首歌,当 webview 加载时。然后你需要调用 play 函数

 `- (void)webViewDidFinishLoad:(UIWebView *)webView {

    // Do whatever you want here
   // play song
}` 

但请确保您已像这样将委托分配给您的网络视图

<UIWebView_object>.delegate = self;

这将在 webview 加载时自动播放歌曲

享受编程!!

于 2013-03-23T06:55:44.507 回答
1

您可以在 Web 视图中加载的网页的 javascript 中设置“自动播放”“开启”。

于 2013-03-26T07:30:12.070 回答
1
- (void)videoThumb:(NSString *)urlString 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, urlString, frame.size.width,frame.size.height];

  [webView loadHTMLString:html baseURL:nil];
  [self.view addSubview:webView];
  [webView release];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //Include above function to your page and call it by passing the urlstring
    //frame : this parameter needs the size of the thumb you want to use.
    [self videoThumb:url:frame];
}

只需通过传递您想要播放的 url 来调用 videoThumb 函数并完成。

于 2013-03-28T07:27:37.883 回答
1

您基于 HTML 5 Youtube 视频在视图中查找按钮的方法。
我为您的问题找到了解决方法,它并不优雅,但可以工作。

你的链接

http://link.brightcove.com/services/player/bcpid1683318714001?bckey=AQ~~,AAAAC59qSJk~,vyxcsD3OtBPHZ2UIrFX2-wdCLTYNyMNn&bclid=1644543007001&bctid=2238216269001

下载带有 Flash 对象的页面。据我所知,没有办法开始使用 JS 或UIWebView.
在研究了请求后,我看到UIWebView了另一个 url 的查询:

http://c.brightcove.com/services/viewer/htmlFederated?&width=966&height=546&flashID=myExperience&bgcolor=%23FFFFFF&playerID=1683318714001&playerKey=AQ~~%2CAAAAC59qSJk~%2CvyxcsD3OtBPHZ2UIrFX2-wdCLTYNyMNn&isVid=true&isUI=true&dynamicStreaming=true&autoStart=true&debuggerID=&videoID= 2238216269001&%40videoPlayer=2238216269001&lineupID=1644543007001&startTime=1364279975091&refURL=not%20available&%40videoPlayer=2238216269001

player第二个更有趣,因为我们可以使用 JavaScript访问变量并启动视频。

所以我们需要转换初始 URL。似乎我们在 URL 中只有 2 个重要参数 -bckeybctid

NSString *urlString = @"http://link.brightcove.com/services/player/bcpid1683318714001?bckey=AQ~~,AAAAC59qSJk~,vyxcsD3OtBPHZ2UIrFX2-wdCLTYNyMNn&bclid=1644543007001&bctid=2238216269001";

NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"bckey=([^&]*)" options:0 error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, urlString.length)];
NSString *bckey = [urlString substringWithRange:[match rangeAtIndex:1]];

regex = [NSRegularExpression regularExpressionWithPattern:@"bctid=([0-9]*)" options:0 error:&error];
match = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, urlString.length)];
NSString *bctid = [urlString substringWithRange:[match rangeAtIndex:1]];

NSString *newUrlString = [NSString stringWithFormat:@"http://c.brightcove.com/services/viewer/htmlFederated?&playerKey=%@&videoID=%@", bckey, bctid];

并加载新的 URL

[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:newUrlString]]];

加载 URL 后,我们可以访问player对象。该对象可以判断视频何时可以播放并开始播放

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if (!webView.isLoading)
    {
        [self checkForVideoReady];
    }

}
// Checking player object for ready. If it ready - starting video. Otherwise waiting 1 second 
- (void)checkForVideoReady
{
    if (![[_webView stringByEvaluatingJavaScriptFromString:@"player.ready"] isEqualToString:@"true"])
    {
        [self performSelector:@selector(checkForVideoReady) withObject:nil afterDelay:1];
    }
    else
    {
        [_webView stringByEvaluatingJavaScriptFromString:@"player.videoPlayer.playButton.element.click();"];
    }
}

我知道这并不优雅,也许有人会找到改进它的方法。
随意发表评论。

于 2013-03-26T07:09:11.417 回答