问题解决:iOS6下UIWebView嵌vedio标签播放视频屏幕不旋转

来源:互联网 发布:经典算法详解 编辑:程序博客网 时间:2024/05/22 15:06

项目默认为竖屏,在iOS6下UIWebView嵌Vedio标签播放视频屏幕不旋转,iOS5没有问题。


一、版本判断宏

// iOS版本===#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)// ===iOS版本


二、首先,在UIWebView所在controller下面开启屏幕旋转支持,iOS5点击UIWebView中vedio标签,播放器可以自动旋转;iOS6不行

#pragma mark - Rotation// IOS5支持的屏幕方向- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}// IOS6开启旋转- (BOOL)shouldAutorotate {    return YES;}// IOS6支持的屏幕方向- (NSUInteger)supportedInterfaceOrientations {    return UIInterfaceOrientationMaskAllButUpsideDown;}// IOS6默认支持竖屏- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {    return UIInterfaceOrientationPortrait;}
三、接着,为了解决iOS6下点击UIWebView中vedio标签,播放器可以不旋转的问题,需要注册播放器进入全屏和退出全屏的通知

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6")) {        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vedioStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];// 播放器即将播放通知        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vedioFinished:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];// 播放器即将退出通知    }

四、再接着,通知事件处理,手工旋转屏幕,播放设为横屏左,退出设为竖屏——这里使用了私有api,有审核不过风险。

- (void)vedioStarted:(NSNotification *)notification {// 播放器即将播放处理    [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIDeviceOrientationLandscapeLeft];// iOS6只能选择一个横屏方向,暂时实现不了检测屏幕旋转而旋转}- (void)vedioFinished:(NSNotification *)notification {// 视频    [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIDeviceOrientationPortrait];// 屏幕方向回复默认的竖屏}

五、最后,记得移除通知的观察者

- (void)dealloc {    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6")) {        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];    }}

参考:http://www.cocoachina.com/bbs/read.php?tid=137339&keyword=UIWebView%7Cvideo和http://stackoverflow.com/a/8554040/966127


完整代码:里面用到UIWebView开源浏览器SVWebViewController(下载地址:https://github.com/samvermette/SVWebViewController)

BLWebVC.h

#import <UIKit/UIKit.h>@class SVWebViewController;@interface BLWebVC : UINavigationController- (id)initWithAddress:(NSString*)urlString;- (id)initWithURL:(NSURL *)URL;@property (nonatomic, retain) UIColor *barsTintColor;@end


BLWebVC.m

////  BLWebVC.m//  XXX////  Created by yhw on 13-3-29.////#import "BLWebVC.h"#import "SVWebViewController.h"@interface BLWebVC ()@property (nonatomic, retain) SVWebViewController *webViewController;@end@implementation BLWebVC- (void)dealloc {    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6")) {        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];    }    [_barsTintColor release];    //    [_webViewController release];    [super dealloc];}- (void)loadView {    [super loadView];    ////    self.view.backgroundColor = [UIColor whiteColor];}- (void)viewDidLoad{    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6")) {        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vedioStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];// 播放器即将播放通知        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vedioFinished:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];// 播放器即将退出通知    }}- (void)vedioStarted:(NSNotification *)notification {// 播放器即将播放处理    [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIDeviceOrientationLandscapeLeft];// iOS6只能选择一个横屏方向,暂时实现不了检测屏幕旋转而旋转}- (void)vedioFinished:(NSNotification *)notification {// 视频    [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIDeviceOrientationPortrait];// 屏幕方向回复默认的竖屏}#pragma mark - Initialization- (id)initWithAddress:(NSString*)urlString {    return [self initWithURL:[NSURL URLWithString:urlString]];}- (id)initWithURL:(NSURL *)URL {    self.webViewController = [[[SVWebViewController alloc] initWithURL:URL] autorelease];    if (self = [super initWithRootViewController:self.webViewController]) {        self.webViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:webViewController action:@selector(doneButtonClicked:)] autorelease];    }    return self;}#pragma mark - Setters- (void)setBarsTintColor:(UIColor *)barsTintColor {    if (_barsTintColor != barsTintColor) {        [_barsTintColor release];        _barsTintColor = [barsTintColor retain];    }    self.navigationBar.tintColor = _barsTintColor;}#pragma mark - Rotation// IOS5支持的屏幕方向- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}// IOS6开启旋转- (BOOL)shouldAutorotate {    return YES;}// IOS6支持的屏幕方向- (NSUInteger)supportedInterfaceOrientations {    return UIInterfaceOrientationMaskAllButUpsideDown;}// IOS6默认支持竖屏- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {    return UIInterfaceOrientationPortrait;}- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];}- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];}- (void)handleOrientationChangeNotification:(NSNotification *)notification{    }@end




原创粉丝点击