iOS开发——获取UIWebView中视频的长度与播放进度等信息
2013-01-15 23:03
507 查看
在做一个upnp项目时,有在线播放视频的模块。使用的是在UIWebView中打开url链接的方式。碰到一个需求,需要获取web页面中视频的长度与及时播放进度。参考微软提供的html5视频控制相关文章后,自己写了一个UIWebView的扩展。经真机测试可用。现提供方法供广大同志们使用(敬请保留版权信息)。
.h文件
[plain]
view plaincopy
//
// UIWebView+VideoControl.h
// Enjoy
//
// Created by zeng songgen on 12-10-15.
// Copyright (c) 2012年 zeng songgen. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIWebView (VideoControl)
- (BOOL)hasVideo;
- (NSString *)getVideoTitle;
- (double)getVideoDuration;
- (double)getVideoCurrentTime;
- (int)play;
- (int)pause;
- (int)resume;
- (int)stop;
@end
.m文件
[plain]
view plaincopy
<pre name="code" class="plain">//
// 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(\"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(\"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(\"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(\"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(\"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(\"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(\"video\")[0].play()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"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(\"video\")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"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(\"video\")[0].play()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"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(\"video\")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
}
}
return 0;
}
@end
.h文件
[plain]
view plaincopy
//
// UIWebView+VideoControl.h
// Enjoy
//
// Created by zeng songgen on 12-10-15.
// Copyright (c) 2012年 zeng songgen. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIWebView (VideoControl)
- (BOOL)hasVideo;
- (NSString *)getVideoTitle;
- (double)getVideoDuration;
- (double)getVideoCurrentTime;
- (int)play;
- (int)pause;
- (int)resume;
- (int)stop;
@end
.m文件
[plain]
view plaincopy
<pre name="code" class="plain">//
// 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(\"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(\"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(\"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(\"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(\"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(\"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(\"video\")[0].play()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"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(\"video\")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"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(\"video\")[0].play()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"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(\"video\")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
NSString * requestDurationString = @"document.documentElement.getElementsByTagName(\"video\")[0].pause()";
[self stringByEvaluatingJavaScriptFromString:requestDurationString];
}
}
return 0;
}
@end
相关文章推荐
- iOS开发——获取UIWebView中视频的长度与播放进度等信息
- iOS开发——获取UIWebView中视频的长度与播放进度等信息
- iOS开发——获取UIWebView中视频的长度与播放进度等信息
- 播放与暂停UIWebView中视频,并获取的长度与播放进度,
- iOS-获取视频长度和当前播放时间
- iOS开发:获取一个视频、音频文件的播放时长
- C#获取 Flv视频文件播放时间长度等信息
- ios开发视频播放后台下载功能实现 :1,ios播放视频 ,包含基于AVPlayer播放器,2,实现下载,iOS后台下载(多任务同时下载,单任务下载,下载进度,下载百分比,文件大小,下载状态)(真机调试功能正常)
- ios 编程: ipad 和 iphone开发中使用 UIWebView 来播放视频的时候,在页面切换时 停止播放
- iOS-获取视频长度和当前播放时间
- ios开发之Swift获取视频截图(本地视频、网络视频的缩略图)
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- ios开发之获取手机通讯录中所有联系人的信息二
- 网上搜集到iOS开发中获取手机IDFA,手机序列号,设备名称,别名,分辨率等信息,这里做个总结
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- iOS开发-获取设备型号信息
- iOS开发系列–音频播放、录音、视频播放、拍照、视频录制