UIWebView 通过JS控制页面视频播放

.h文件

[plain]  view plain copy
  1. //  
  2. //  UIWebView+VideoControl.h  
  3. //  Enjoy  
  4. //  
  5. //  Created by zeng songgen on 12-10-15.  
  6. //  Copyright (c) 2012年 zeng songgen. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface UIWebView (VideoControl)  
  12.   
  13.   
  14. - (BOOL)hasVideo;  
  15. - (NSString *)getVideoTitle;  
  16. - (double)getVideoDuration;  
  17. - (double)getVideoCurrentTime;  
  18.   
  19. - (int)play;  
  20. - (int)pause;  
  21. - (int)resume;  
  22. - (int)stop;  
  23.   
  24. @end  



.m文件

[plain]  view plain copy
  1. <pre name="code" class="plain">//  
  2. //  UIWebView+VideoControl.m  
  3. //  Enjoy  
  4. //  
  5. //  Created by zeng songgen on 12-10-15.  
  6. //  Copyright (c) 2012年 zeng songgen. All rights reserved.  
  7. //  
  8.   
  9. #import "UIWebView+VideoControl.h"  
  10.   
  11. @implementation UIWebView (VideoControl)  
  12.   
  13. - (BOOL)hasVideo  
  14. {  
  15.     __block BOOL hasVideoTag = NO;  
  16.   
  17.     if (![[NSThread currentThread] isMainThread])  
  18.     {  
  19.         dispatch_semaphore_t sema = dispatch_semaphore_create(0);  
  20.           
  21.         dispatch_async(dispatch_get_main_queue(), ^{  
  22.             NSString * hasVideoTestString = @"document.documentElement.getElementsByTagName(\"video\").length";  
  23.             NSString * result = [self stringByEvaluatingJavaScriptFromString:hasVideoTestString];  
  24.             hasVideoTag = [result integerValue] >= 1? YES : NO;  
  25.               
  26.             dispatch_semaphore_signal(sema);  
  27.         });  
  28.         dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  
  29.     }  
  30.     else  
  31.     {  
  32.         NSString * hasVideoTestString = @"document.documentElement.getElementsByTagName(\"video\").length";  
  33.         NSString * result = [self stringByEvaluatingJavaScriptFromString:hasVideoTestString];  
  34.         hasVideoTag = [result integerValue] >= 1? YES : NO;  
  35.     }  
  36.       
  37.       
  38.     return hasVideoTag;  
  39. }  
  40. -(NSString *)getVideoTitle  
  41. {  
  42.     __block NSString * title = nil;  
  43.       
  44.     if (![[NSThread currentThread] isMainThread])  
  45.     {  
  46.         dispatch_semaphore_t sema = dispatch_semaphore_create(0);  
  47.           
  48.         dispatch_async(dispatch_get_main_queue(), ^{  
  49.             NSString *currentURL = [self stringByEvaluatingJavaScriptFromString:@"document.location.href"];  
  50.             title = [self stringByEvaluatingJavaScriptFromString:@"document.title"];  
  51.             NSLog(@"++++ URL:%@",currentURL);  
  52.             NSLog(@"++++ title:%@", title);  
  53.               
  54.             dispatch_semaphore_signal(sema);  
  55.         });  
  56.         dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  
  57.     }  
  58.     else  
  59.     {  
  60.         NSString *currentURL = [self stringByEvaluatingJavaScriptFromString:@"document.location.href"];  
  61.         title = [self stringByEvaluatingJavaScriptFromString:@"document.title"];  
  62.         NSLog(@"++++ URL:%@",currentURL);  
  63.         NSLog(@"++++ title:%@", title);  
  64.     }  
  65.   
  66.       
  67.     return title;  
  68. }  
  69. - (double)getVideoDuration  
  70. {  
  71.     __block double duration = 0;  
  72.       
  73.     if ([self hasVideo])  
  74.     {  
  75.         if (![[NSThread currentThread] isMainThread])  
  76.         {  
  77.             dispatch_semaphore_t sema = dispatch_semaphore_create(0);  
  78.               
  79.             dispatch_async(dispatch_get_main_queue(), ^{  
  80.                 NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].duration.toFixed(1)";  
  81.                 NSString * result = [self stringByEvaluatingJavaScriptFromString:requestDurationString];  
  82.                 NSLog(@"+++ Web Video Duration:%@",result);  
  83.                 duration = [result doubleValue];  
  84.                   
  85.                 dispatch_semaphore_signal(sema);  
  86.             });  
  87.             dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  
  88.         }  
  89.         else  
  90.         {  
  91.             NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].duration.toFixed(1)";  
  92.             NSString * result = [self stringByEvaluatingJavaScriptFromString:requestDurationString];  
  93.             NSLog(@"+++ Web Video Duration:%@",result);  
  94.             duration = [result doubleValue];  
  95.         }  
  96.     }  
  97.       
  98.     return duration;  
  99. }  
  100. - (double)getVideoCurrentTime  
  101. {  
  102.     __block double currentTime = 0;  
  103.       
  104.     if ([self hasVideo])  
  105.     {  
  106.         if (![[NSThread currentThread] isMainThread])  
  107.         {  
  108.             dispatch_semaphore_t sema = dispatch_semaphore_create(0);  
  109.               
  110.             dispatch_async(dispatch_get_main_queue(), ^{  
  111.                 NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].currentTime.toFixed(1)";  
  112.                 NSString * result = [self stringByEvaluatingJavaScriptFromString:requestDurationString];  
  113.                 NSLog(@"+++ Web Video CurrentTime:%@",result);  
  114.                 currentTime = [result doubleValue];  
  115.                   
  116.                 dispatch_semaphore_signal(sema);  
  117.             });  
  118.             dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  
  119.         }  
  120.         else  
  121.         {  
  122.             NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].currentTime.toFixed(1)";  
  123.             NSString * result = [self stringByEvaluatingJavaScriptFromString:requestDurationString];  
  124.             NSLog(@"+++ Web Video CurrentTime:%@",result);  
  125.             currentTime = [result doubleValue];  
  126.         }  
  127.     }  
  128.       
  129.     return currentTime;  
  130. }  
  131.   
  132. - (int)play  
  133. {  
  134.     if ([self hasVideo])  
  135.     {  
  136.         if (![[NSThread currentThread] isMainThread])  
  137.         {  
  138.             dispatch_semaphore_t sema = dispatch_semaphore_create(0);  
  139.               
  140.             dispatch_async(dispatch_get_main_queue(), ^{  
  141.                 NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].play()";  
  142.                 [self stringByEvaluatingJavaScriptFromString:requestDurationString];  
  143.                   
  144.                 dispatch_semaphore_signal(sema);  
  145.             });  
  146.             dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  
  147.         }  
  148.         else  
  149.         {  
  150.             NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].play()";  
  151.             [self stringByEvaluatingJavaScriptFromString:requestDurationString];  
  152.         }  
  153.     }  
  154.     return 0;  
  155. }  
  156. - (int)pause  
  157. {  
  158.     if ([self hasVideo])  
  159.     {  
  160.         if (![[NSThread currentThread] isMainThread])  
  161.         {  
  162.             dispatch_semaphore_t sema = dispatch_semaphore_create(0);  
  163.               
  164.             dispatch_async(dispatch_get_main_queue(), ^{  
  165.                 NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].pause()";  
  166.                 [self stringByEvaluatingJavaScriptFromString:requestDurationString];  
  167.                   
  168.                 dispatch_semaphore_signal(sema);  
  169.             });  
  170.             dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  
  171.         }  
  172.         else  
  173.         {  
  174.             NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].pause()";  
  175.             [self stringByEvaluatingJavaScriptFromString:requestDurationString];  
  176.         }  
  177.     }  
  178.     return 0;  
  179. }  
  180. - (int)resume  
  181. {  
  182.     if ([self hasVideo])  
  183.     {  
  184.         if (![[NSThread currentThread] isMainThread])  
  185.         {  
  186.             dispatch_semaphore_t sema = dispatch_semaphore_create(0);  
  187.               
  188.             dispatch_async(dispatch_get_main_queue(), ^{  
  189.                 NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].play()";  
  190.                 [self stringByEvaluatingJavaScriptFromString:requestDurationString];  
  191.                   
  192.                 dispatch_semaphore_signal(sema);  
  193.             });  
  194.             dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  
  195.         }  
  196.         else  
  197.         {  
  198.             NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].play()";  
  199.             [self stringByEvaluatingJavaScriptFromString:requestDurationString];  
  200.         }  
  201.     }  
  202.     return 0;  
  203. }  
  204. - (int)stop  
  205. {  
  206.     if ([self hasVideo])  
  207.     {  
  208.         if (![[NSThread currentThread] isMainThread])  
  209.         {  
  210.             dispatch_semaphore_t sema = dispatch_semaphore_create(0);  
  211.               
  212.             dispatch_async(dispatch_get_main_queue(), ^{  
  213.                 NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].pause()";  
  214.                 [self stringByEvaluatingJavaScriptFromString:requestDurationString];  
  215.                   
  216.                 dispatch_semaphore_signal(sema);  
  217.             });  
  218.             dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  
  219.         }  
  220.         else  
  221.         {  
  222.             NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].pause()";  
  223.             [self stringByEvaluatingJavaScriptFromString:requestDurationString];  
  224.         }  
  225.     }  
  226.       
  227.     return 0;  
  228. }  
  229.   
  230.   
  231. @end  
  232. </pre><br>  
  233. <br>  
  234. <pre></pre>  
更多 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值