web頁面中的視頻橫豎屏

import "ViewController.h"

import "AppDelegate.h"

@interface ViewController ()<UIWebViewDelegate>{
AppDelegate *app;
}

@end

@implementation ViewController

  • (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeHiddenNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeVisibleNotification object:nil];
    }

-(void)playerWillExitFullscreen:(id)sender{
NSLog(@"退出播放視頻了");
app.isFull=NO;

/**
 下邊方法的使用場景:
 如果點擊視頻,自動旋轉為橫屏播放狀態,點擊完成按鈕,需要是程序變為豎屏狀態,需要下邊的代碼
 */
UIViewController *vc = [[UIViewController alloc]init];
[self presentViewController:vc animated:NO completion:nil];
[vc dismissViewControllerAnimated:NO completion:nil];

}
-(void)playerWillShowFullScreen:(id)sender{
NSLog(@"播放視頻了");
app.isFull=YES;
}

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.title=@"webView";
    CGRect rect=self.view.frame;
    // rect.size.height-=64;
    UIWebView *webView=[[UIWebView alloc] initWithFrame:rect];
    webView.delegate=self;
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.tudou.com/albumplay/O8GDpd7v8RA/qTfiUJAEdm0.html"]]];
    [self.view addSubview:webView];

    app=(AppDelegate *)[UIApplication sharedApplication].delegate;
    }

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

  • (void)webViewDidFinishLoad:(UIWebView *)webView{

//通知寫在這里是因為網頁加載完成但是沒有播放視頻,也會調用playerWillExitFullscreen方法

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeHiddenNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeVisibleNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerWillExitFullscreen:) name:UIWindowDidBecomeHiddenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerWillShowFullScreen:) name:UIWindowDidBecomeVisibleNotification object:nil];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容