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