播放与暂停UIWebView中视频,并获取的长度与播放进度,

转自: http://blog.sina.com.cn/s/blog_63b4ee0d0101gd0s.html

如有侵犯,请来信oiken@qq.com


碰到一个需求,需要获取web页面中视频的长度与及时播放进度。参考微软提供的html5视频控制相关文章后,写了一个UIWebView的扩展。经真机测试可用。
.h文件

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值