開發(fā)過程中,老板要求手機播放視頻時能夠全屏播放的時候自動橫屏,以增加用戶體驗,于是就用到了偉大的度娘,下面是給我?guī)椭奶渔溄?,同事感謝樓主,但是在使用過程中遇到一個問題,我就稍微改了一下,這個要看每個人的實際代碼。原帖鏈接:http://www.cocoachina.com/bbs/read.php?tid=456927
看完以后,作為菜鳥,我也就直接復制粘貼了,但是運行時遇到一個奇葩問題:點擊播放第一個視頻,全屏,橫屏,ok,沒問題,但是點擊第二個就不能自動橫屏了。經(jīng)過測試,奇數(shù)次點擊可以橫屏,偶數(shù)次就不能橫屏,那么問題來了,有規(guī)律??!有規(guī)律就簡單了,廢話這么多,直接上代碼
下面是我自己用到的代碼
1、在創(chuàng)建完視屏播放器的時候注冊兩個系統(tǒng)通知
//將要進入全屏的通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(willEnterFullscreenScreen:)name:MPMoviePlayerWillEnterFullscreenNotificationobject:nil];
//將要推出全屏的通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(willExitFullscreenScreen:)name:MPMoviePlayerWillExitFullscreenNotificationobject:nil];
2、實現(xiàn)通知方法
//將要進入全屏
-(void)willEnterFullscreenScreen:(NSNotification*)notification{
[[UIDevicecurrentDevice]setValue:[NSNumbernumberWithInteger:UIInterfaceOrientationLandscapeRight]forKey:@"orientation"];
}
//將要推出全屏
-(void)willExitFullscreenScreen:(NSNotification*)notification{
[[UIDevicecurrentDevice]setValue:[NSNumbernumberWithInteger:UIInterfaceOrientationPortrait]forKey:@"orientation"];
}
3、搞定!是不是很簡單,也不需要釋放、移除什么的,其實我也就是刪除了判斷(雖然我不知道判斷是做什么用的,本人菜鳥一枚),然后就實現(xiàn)了自己想要的效果