上一篇:iOS使用AVPlayer自定義音頻播放器
iOS內(nèi)部提供的有三種
視頻播放的方式灵汪,且都能播放本地蔗候、遠(yuǎn)程
的音頻怒允、視頻
文件。
- AVPlayer锈遥,基于Layer顯示纫事,具有高度的
靈活性
勘畔,可根據(jù)需要自定義UI,實(shí)現(xiàn)起來(lái)相對(duì)麻煩
丽惶。 - MPMoviePlayerController炫七,自帶播放控制面板,
可控性低
蚊夫。 - MPMoviePlayerViewController诉字,
高度封裝
,播放界面默認(rèn)就是全屏的知纷,如果播放功能比較簡(jiǎn)單
壤圃,建議用這個(gè)。
這篇文章琅轧,我們主要講一下第一種播放方式<AVPlayer>
伍绳,畢竟后兩種方式的可控性太低了,根本滿足不了我們蛋疼的項(xiàng)目經(jīng)理
的要求乍桂。
LLVideoPlayer-橫屏.png
LLVideoPlayer-豎屏.png
蛋疼的項(xiàng)目經(jīng)理
讓我們模仿優(yōu)酷冲杀、模仿愛(ài)奇藝
,可憐我們做的只是一款社交聊天軟件
睹酌,嗚呼哀哉~
我寫(xiě)了一個(gè)簡(jiǎn)單的demo权谁,頁(yè)面相當(dāng)精簡(jiǎn),但是該實(shí)現(xiàn)的功能都實(shí)現(xiàn)了憋沿,左邊調(diào)節(jié)亮度
旺芽、右邊調(diào)節(jié)聲音
等等也都實(shí)現(xiàn)了,有興趣的童鞋可以下載看一下辐啄。
延伸知識(shí)
我們的項(xiàng)目是這種要求采章,只有在播放視頻的頁(yè)面才支持橫屏
,其他頁(yè)面都是豎屏
壶辜,因此悯舟,需要做以下處理,首先:
屏幕快照 2017-04-15 下午7.27.37.png
然后砸民,
重點(diǎn)中的重點(diǎn)
抵怎,在AppDelegate中實(shí)現(xiàn)如下方法:屏幕快照 2017-04-15 下午7.30.11.png
//只支持豎屏
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskPortrait;
}
之后在需要橫屏的試圖控制器中,監(jiān)聽(tīng)當(dāng)前手機(jī)的橫豎屏狀態(tài)岭参,使用代碼強(qiáng)制旋轉(zhuǎn)屏幕
:
- (void)viewDidLoad {
[super viewDidLoad];
//監(jiān)聽(tīng)橫豎屏切換
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
//橫豎屏切換
- (void)orientationChanged:(NSNotification *)notification {
UIDeviceOrientation currentOrientation = [UIDevice currentDevice].orientation;
if (currentOrientation == UIDeviceOrientationFaceUp) return;
static CGFloat rotation;
if (currentOrientation == UIDeviceOrientationLandscapeLeft) {
rotation = 0.5;
}
else if (currentOrientation == UIDeviceOrientationLandscapeRight) {
rotation = -0.5;
}
else {
rotation = 0;
}
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformMakeRotation(M_PI*(rotation));
self.view.frame = SCREEN_BOUNDS;
}];
});
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
播放過(guò)程中的具體細(xì)節(jié)處理便贵,demo里面注釋的很詳細(xì),不懂的地方歡迎留言冗荸。