這次的版本開發無疑是對我的一次挑戰,所有的新功能都需要獨立開發,并且時間很緊,之前連視頻播放器都很少接觸,但新需求一來就是直播功能,真的還是蠻有壓力的。不過完后工回想一下收獲頗多,無論是技術上的,還是心理上的。吼吼,來總結一下此次開發中踩過的坑吧!
第一大坑: 局部界面轉屏
iOS開發開發中,轉屏包含設備的轉動方向和屏幕轉動方向,如果要開啟設備的轉屏方向,可以在Target中勾選多支持的方向,但是我們的項目比較特殊,只有在播放視頻的界面才可以轉屏其他界面只能豎屏,這個需求困擾了我好久,終于最后找到了一個解決方案:
首先在Target中勾選所需要支持的轉屏方向
在 AppDelegate.h 中聲明一個屬性用來標示是否可以轉屏
/*** 是否允許橫屏的標記 */
@property (nonatomic,assign)BOOL allowRotation;
- 在 AppDelegate.m 中 實現方法:
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.allowRotation) {
return UIInterfaceOrientationMaskPortrait| UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}else {
return UIInterfaceOrientationMaskPortrait;
}
}
與之相關的還有兩個方法如下,但是我這里不需要就沒有實現
//是否自動旋轉,返回YES可以自動旋轉
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
//這個是返回優先方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
- 然后在你需要轉屏的那個 ViewController 里面設置轉屏與否
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
}
需要注意的是如果在這個視圖控制器消失或銷毀后,保持其他視圖控制器不支持轉屏,那你需要在viewWillDisAppear的時候將AppDelegate中的屬性設置為NO
- (void)viewWillDisappear:(BOOL)animated {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = NO;
[self.playerView shutDownPlayer];
[UIApplication sharedApplication].statusBarHidden = NO;
}
- 在需要轉屏的ViewController中添加通知監測設備是否旋轉,并針對不同方向設置做不同的操作
- (void)listeningRotating {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onDeviceOrientationChange)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)onDeviceOrientationChange {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
UIInterfaceOrientation interfaceOrientation =
(UIInterfaceOrientation)orientation;
[self.landscapeControlOverlay removeFromSuperview];
switch (interfaceOrientation) {
case UIInterfaceOrientationPortrait:
[self setOrientationPortrait];
break;
case UIInterfaceOrientationLandscapeRight:
[self setOrientationLandscape];
case UIInterfaceOrientationLandscapeLeft:
[self setOrientationLandscape];
default:
break;
}
}
- 強制屏幕旋轉
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation {
if ([[UIDevice currentDevice]
respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation
invocationWithMethodSignature:
[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
if (orientation == UIInterfaceOrientationLandscapeRight ||
orientation == UIInterfaceOrientationLandscapeLeft) {
[self setOrientationLandscape];
} else if (orientation == UIInterfaceOrientationPortrait) {
[self setOrientationPortrait];
}
}
第二大坑:轉屏后遺癥
通過才第一個坑終于把視頻播放界面轉屏其他界面只能豎屏的功能實現了,接著迎接的就是其他界面無緣無故的 crash,說具體一點我們不僅只有一個觀看直播的界面,還可以在其他界面的tableViewCell中點擊觀看,但是我將轉屏通知,轉屏后的界面更新,通通都封裝在視頻播放器這個控件中,但是其他界面tableViewCell中的播放器卻又不支持轉屏功能。所以,當我在設置中取消屏幕鎖定后,然后在cell中播放視頻時,只要一晃動手機就會崩潰在MAsonry中提示: couldn't find a common superview for <UIButton: 0x14d172340; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x1510b0690>> and <LivePlayer: 0x14cf889a0; frame = (0 0; 0 0); layer = <CALayer: 0x14f60e740>>
這個問題可是讓我足足找了兩天,真的是整的我和測試妹子都要崩潰了。其原因就是在tableview所在的這個ViewController中是不支持轉屏的,但是我在初始化播放器控件的時候并沒有判斷當前界面是否支持轉屏,不分青紅皂白的給人家通通設置了設備轉屏通知,就是上面提到的 - (void)listeningRotating
方法,所以我只要一晃動手機就會出發改通知方法,給一些不存在的對象設置約束,當然要崩潰了。所以提醒大家也給自己敲個警鐘,以后千萬要注意了。
第三大坑:Cell中的播放器最好使用單例
如果需要在TableViewCell中播放視頻,建議將視頻播放器寫成一個單例,不然真的很麻煩,我是把所有功能都開發完了才發現如果用單例代碼會更簡潔。
現在我的做法是當你在某個cell中播放視頻,然后再點擊其他cell中的視頻時,將上一條關閉;如果在cell播放視頻的時候切換到其他界面時,將當前cell中的播放器關閉;程序進入后臺時關閉當前cell中的直播;從直播播放切換到音頻播放時,關閉直播;這些情況都需要記錄當前cell,然后在對cell中的播放器做操作,這樣做其實也沒什么問題,但是不夠優雅,并且消耗資源,每次都需要記錄當前的cell所在的indexpath,如果使用單里的話無論在什么地方都可以對播放器進行操作,不僅方便還提高了代碼的易讀性。
- (void)shutDownSelectCellPlayer {
if (_currentIndex != 0) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_currentIndex-1000 inSection:0];
LivePlayerCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell stopLivePlayer];
}
}
我這是一發反面教材,以后還是要改成單例模式
第四大坑: ARC下的僵尸指針問題
這個坑我不知道要怎么描述,因為我封裝的播放器邏輯不夠嚴謹,因此有時會給一個已經釋放了的對象做操作造成致命的崩潰
第五大坑: 邏輯嚴謹性
這點我不知該從何說起或者怎么說,或許還是因為自己對于有些基礎掌握的不牢固,因此在開發過程中總會犯些低級錯誤,比如先將某對象釋放了,然后又對其發送了消息,或者是在某對象可能還沒創建出來的時候就以該對象為參照視圖使用masonry建立約束,然后花時間去找崩潰原因,找到問題根源后恍然大吾然后自己都服了自己了。所以在寫代碼的時候一定要先將邏輯里清楚,這點很重要,慢慢修煉吧??