【问题标题】:Is It Possible To Interact With UIWebView Without Touch?是否可以在没有触摸的情况下与 UIWebView 交互?
【发布时间】:2013-03-10 02:42:51
【问题描述】:

我正在尝试在不触摸屏幕的情况下与 UIWebView 中加载的页面进行交互。我试过 mediaplaybackrequiresuseraction 没有成功。我也尝试过here提供的方法,但显然它在ios 6中不起作用。我正在尝试加载this 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();"];
   }
 }

【问题讨论】:

  • 这不正是UIWebView 上的mediaPlaybackRequiresUserAction 属性的用途吗?
  • mediaPlaybackRequiresUserAction 由于无法识别的选择器导致崩溃。请检查编辑。
  • 好吧,你必须使用setMediaPlaybackRequiresUserAction:NO。 videoScreen 也必须是 UIWebView
  • 使用 setMediaPlaybackRequiresUserAction:NO 不会导致崩溃,但媒体无法播放
  • 你设置了 UIWebView 的代理吗?

标签: iphone html ios url uiwebview


【解决方案1】:

如果你只想播放一首歌,当 webview 加载时。然后你需要在

中调用play函数
 `- (void)webViewDidFinishLoad:(UIWebView *)webView {

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

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

<UIWebView_object>.delegate = self;

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

享受编程!

【讨论】:

  • 感谢您的回复。我不明白你的答案与我已经在做的有什么不同。你能解释一下吗?
  • @B.Money ,是的,无论我从您的问题中了解到什么,您都想在不触摸 UIWebView 的情况下播放一首歌曲,即在应用加载后立即播放,对吗?
【解决方案2】:

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

你的链接

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

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

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

并加载新网址

[_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();"];
    }
}

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

【讨论】:

  • 感谢您的回复。您的回答很有意义,但是当我实施它时它不起作用。有什么我想念的吗?请检查编辑。
【解决方案3】:

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

【讨论】:

    【解决方案4】:
    - (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 函数即可。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多