5

I'm trying to interact with a loaded page in a UIWebView without touching the screen. I have tried mediaplaybackrequiresuseraction with no success. I have also tried the method provided here, but apparently it doesn't work in ios 6. I'm trying to load this url and make the player start playing. Does anyone know how to make this work ios 6? Any help is greatly appreciated.

MY CODE

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

 }

//Method not working in 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];
 }

EDIT //videoScreen is my 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();"];
   }
 }
Community
  • 1
  • 1
B. Money
  • 931
  • 2
  • 19
  • 56

4 Answers4

4

If you want to play a song only, as & when webview loads. Then you need to invoke play function in

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

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

but make sure you have assigned delegate to your webview like this

<UIWebView_object>.delegate = self;

This will automatically play a song as & when webview loads

Enjoy Programming!!

Niru Mukund Shah
  • 4,637
  • 2
  • 20
  • 34
  • Thanks for responding. I don't understand how your answer is different from what I am already doing. Could you expound? – B. Money Mar 23 '13 at 23:16
  • @B.Money , Yes,Whatever I understood from your question, you want to play a song from UIWebView without touching it, that is as soon as the app loads, correct? – Niru Mukund Shah Mar 26 '13 at 09:09
1

Your method for find button in view based on HTML 5 Youtube videos.
I found workaround for your problem, it's not elegant, but working.

Your link

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

downloads page with flash object. And as far as I know there is no way to start playing it using JS or UIWebView.
After researching requests I saw that UIWebView query for another 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

The second one is more interesting, because we can access to player variable and start video with using JavaScript.

So we need to convert initial URL. Seems like we have only 2 important parameters in URL - bckey and bctid:

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

And load new URL

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

After URL is loading we have access to player object. This object can tell when video is ready to play and start playback

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

I know that this is not elegant, maybe someone will find a way how to improve it.
Feel free to comment.

Sergey Kuryanov
  • 6,114
  • 30
  • 52
  • Thanks for responding. Your answer makes a lot of sense but it didn't work when I implemented it. Is there something I'm missing? Please check edit. – B. Money Mar 27 '13 at 14:13
1

you ca have "autoplay" "on" in the javascript of the web page you are loading in web view.

Nikita P
  • 4,226
  • 5
  • 31
  • 55
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];
}

Just call the videoThumb function by passing the url you want to play for and just done.

Kuldeep
  • 2,589
  • 1
  • 18
  • 28
  • http://stackoverflow.com/questions/9992735/video-playing-automatically-in-uiwebview/9993698#comment22222207_9993698 refer same question and answer on this link visit it will be helpful to you. – Kuldeep Mar 28 '13 at 10:35