播放与暂停UIWebView中视频,并获取的长度与播放进度,
2015-10-05 20:42
411 查看
转自: http://blog.sina.com.cn/s/blog_63b4ee0d0101gd0s.html
如有侵犯,请来信oiken@qq.com
碰到一个需求,需要获取web页面中视频的长度与及时播放进度。参考微软提供的html5视频控制相关文章后,写了一个UIWebView的扩展。经真机测试可用。
.h文件
//
// UIWebView+VideoControl.h
// Enjoy
//
// Created by zeng songgen on 12-10-15.
// Copyright (c) 2012年 zeng songgen. All rights reserved.
//
#import
@interface UIWebView (VideoControl)
- (BOOL)hasVideo;
- (NSString *)getVideoTitle;
- (double)getVideoDuration;
- (double)getVideoCurrentTime;
- (int)play;
- (int)pause;
- (int)resume;
- (int)stop;
@end
.m文件
// UIWebView+VideoControl.m
// Enjoy
//
// Created by zeng songgen on 12-10-15.
// Copyright (c) 2012年 zeng songgen. All rights reserved.
//
#import "UIWebView+VideoControl.h"
@implementation UIWebView (VideoControl)
- (BOOL)hasVideo
{
__block BOOL hasVideoTag = NO;
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * hasVideoTestString = @"document.documentElement.getElementsByTagName_r("video").length";
NSString * result = [self stringByEvaluatingJavaScriptFromString:hasVideoTestString];
hasVideoTag = [result integerValue] >= 1? YES : NO;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * hasVideoTestString = @"document.documentElement.getElementsByTagName_r("video").length";
NSString * result = [self stringByEvaluatingJavaScriptFromString:hasVideoTestString];
hasVideoTag = [result integerValue] >= 1? YES : NO;
}
return hasVideoTag;
}
-(NSString *)getVideoTitle
{
__block NSString * title = nil;
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString *currentURL = [self stringByEvaluatingJavaScriptFromString:@"document.location.href"];
title = [self stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"++++ URL:%@",currentURL);
NSLog(@"++++ title:%@", title);
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString *currentURL = [self stringByEvaluatingJavaScriptFromString:@"document.location.href"];
title = [self stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"++++ URL:%@",currentURL);
NSLog(@"++++ title:%@", title);
}
return title;
}
- (double)getVideoDuration
{
__block double duration = 0;
if ([self hasVideo])
{
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].duration.toFixed(1)";
NSString * result = [self stringByEvaluatingJavaScriptFromString:requestDurationString];
NSLog(@"+++ Web Video Duration:%@",result);
duration = [result doubleValue];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].duration.toFixed(1)";
NSString * result = [self stringByEvaluatingJavaScriptFromString:requestDurationString];
NSLog(@"+++ Web Video Duration:%@",result);
duration = [result doubleValue];
}
}
return duration;
}
- (double)getVideoCurrentTime
{
__block double currentTime = 0;
if ([self hasVideo])
{
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].currentTime.toFixed(1)";
NSString * result = [self stringByEvaluatingJavaScriptFromString:requestDurationString];
NSLog(@"+++ Web Video CurrentTime:%@",result);
currentTime = [result doubleValue];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].currentTime.toFixed(1)";
NSString * result = [self stringByEvaluatingJavaScriptFromString:requestDurationString];
NSLog(@"+++ Web Video CurrentTime:%@",result);
currentTime = [result doubleValue];
}
}
return currentTime;
}
- (int)play
{
if ([self hasVideo])
{
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].play()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].play()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
}
}
return 0;
}
- (int)pause
{
if ([self hasVideo])
{
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
}
}
return 0;
}
- (int)resume
{
if ([self hasVideo])
{
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].play()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].play()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
}
}
return 0;
}
- (int)stop
{
if ([self hasVideo])
{
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
}
}
return 0;
}
@end
如有侵犯,请来信oiken@qq.com
碰到一个需求,需要获取web页面中视频的长度与及时播放进度。参考微软提供的html5视频控制相关文章后,写了一个UIWebView的扩展。经真机测试可用。
.h文件
//
// UIWebView+VideoControl.h
// Enjoy
//
// Created by zeng songgen on 12-10-15.
// Copyright (c) 2012年 zeng songgen. All rights reserved.
//
#import
@interface UIWebView (VideoControl)
- (BOOL)hasVideo;
- (NSString *)getVideoTitle;
- (double)getVideoDuration;
- (double)getVideoCurrentTime;
- (int)play;
- (int)pause;
- (int)resume;
- (int)stop;
@end
.m文件
// UIWebView+VideoControl.m
// Enjoy
//
// Created by zeng songgen on 12-10-15.
// Copyright (c) 2012年 zeng songgen. All rights reserved.
//
#import "UIWebView+VideoControl.h"
@implementation UIWebView (VideoControl)
- (BOOL)hasVideo
{
__block BOOL hasVideoTag = NO;
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * hasVideoTestString = @"document.documentElement.getElementsByTagName_r("video").length";
NSString * result = [self stringByEvaluatingJavaScriptFromString:hasVideoTestString];
hasVideoTag = [result integerValue] >= 1? YES : NO;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * hasVideoTestString = @"document.documentElement.getElementsByTagName_r("video").length";
NSString * result = [self stringByEvaluatingJavaScriptFromString:hasVideoTestString];
hasVideoTag = [result integerValue] >= 1? YES : NO;
}
return hasVideoTag;
}
-(NSString *)getVideoTitle
{
__block NSString * title = nil;
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString *currentURL = [self stringByEvaluatingJavaScriptFromString:@"document.location.href"];
title = [self stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"++++ URL:%@",currentURL);
NSLog(@"++++ title:%@", title);
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString *currentURL = [self stringByEvaluatingJavaScriptFromString:@"document.location.href"];
title = [self stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"++++ URL:%@",currentURL);
NSLog(@"++++ title:%@", title);
}
return title;
}
- (double)getVideoDuration
{
__block double duration = 0;
if ([self hasVideo])
{
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].duration.toFixed(1)";
NSString * result = [self stringByEvaluatingJavaScriptFromString:requestDurationString];
NSLog(@"+++ Web Video Duration:%@",result);
duration = [result doubleValue];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].duration.toFixed(1)";
NSString * result = [self stringByEvaluatingJavaScriptFromString:requestDurationString];
NSLog(@"+++ Web Video Duration:%@",result);
duration = [result doubleValue];
}
}
return duration;
}
- (double)getVideoCurrentTime
{
__block double currentTime = 0;
if ([self hasVideo])
{
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].currentTime.toFixed(1)";
NSString * result = [self stringByEvaluatingJavaScriptFromString:requestDurationString];
NSLog(@"+++ Web Video CurrentTime:%@",result);
currentTime = [result doubleValue];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].currentTime.toFixed(1)";
NSString * result = [self stringByEvaluatingJavaScriptFromString:requestDurationString];
NSLog(@"+++ Web Video CurrentTime:%@",result);
currentTime = [result doubleValue];
}
}
return currentTime;
}
- (int)play
{
if ([self hasVideo])
{
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].play()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].play()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
}
}
return 0;
}
- (int)pause
{
if ([self hasVideo])
{
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
}
}
return 0;
}
- (int)resume
{
if ([self hasVideo])
{
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].play()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].play()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
}
}
return 0;
}
- (int)stop
{
if ([self hasVideo])
{
if (![[NSThread currentThread] isMainThread])
{
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName_r("video")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
}
}
return 0;
}
@end
相关文章推荐
- android Bitmap总结
- Android,Java编程小常识
- 竞品分析与设计:Apple平板的故事
- 竞品分析与设计:Apple平板的故事
- Matlab-vision包学习-Object Detection and Recognition-标记正例
- Android基础学习之Preference(配置存储)
- iOS编程------singleton_AddressBook 单例模式 通讯录
- 关于蓝牙开发之数据缓存问题(脏数据)
- iOS多线程
- Matlab-vision包学习-Object Detection and Recognition-物体识别方法中级联分类器训练
- IOS---通知事件
- Android 简化findViewById写法
- 如何解决ViewPager中的预加载
- unity官方demo学习之Stealth(二十四)敌人AI
- Android 自定义圆形头像
- Android任务流调度 - Init
- cocos3——8.实现初学者指南
- android自定义组件的简易实现
- android自定义组件的简易实现
- 大数据下的日志--ElasticSearch部分(四)--Mapping