@(iOS Study)[音視頻播放]
- 作者: Liwx
- 郵箱: 1032282633@qq.com
目錄
- 03.音頻視頻播放 音頻視頻播放知識(shí)點(diǎn) (自己整理)
- 音頻/視頻播放
- 1.播放網(wǎng)絡(luò)音樂(lè)(AVPlayer)
- 創(chuàng)建AVPlayer的幾種方式
- 播放網(wǎng)絡(luò)音樂(lè)
- 2.播放網(wǎng)絡(luò)視頻(AVPlayer)
- 使用AVPlayer實(shí)現(xiàn)網(wǎng)絡(luò)視頻的播放
- 3.MPMoviePlayerController視頻播放器控制器實(shí)現(xiàn)播放視頻
- MPMoviePlayerController播放視頻
- 4.MPMoviePlayerViewController播放網(wǎng)絡(luò)視頻
- MPMoviePlayerViewController播放視頻
- 5.使用AVPlayerViewController播放網(wǎng)絡(luò)視頻
- 使用AVKit/AVKit框架中的AVPlayerViewController播放視頻
- 6.Vitamio SDK的集成
- Vitamio SDK功能介紹
- 7.SizeClasses的簡(jiǎn)單使用
- 屏幕適配
- SizeClasses簡(jiǎn)述
音頻/視頻播放
1.播放網(wǎng)絡(luò)音樂(lè)(AVPlayer)
創(chuàng)建AVPlayer的幾種方式
- 通過(guò)url創(chuàng)建AVPlayer播放器,使用playerWithURL方法創(chuàng)建播放器
- 通過(guò)AVPlayerItem創(chuàng)建AVPlayer播放器,使用playerWithPlayerItem方法創(chuàng)建播放器
播放網(wǎng)絡(luò)音樂(lè)
- 播放網(wǎng)絡(luò)音樂(lè),實(shí)現(xiàn)播放網(wǎng)絡(luò)音樂(lè),切換音樂(lè)
- 切換音樂(lè),使用replaceCurrentItemWithPlayerItem切換音樂(lè)
NSURL *url = [NSURL URLWithString:@"http://tsmusic128.tc.qq.com/37023937.mp3"];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
[self.player replaceCurrentItemWithPlayerItem:item];
- 參考代碼
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
/** 創(chuàng)建播放器 */
@property (nonatomic, strong) AVPlayer *player;
@end
@implementation ViewController
- (IBAction)playOneLine {
// 播放音樂(lè)
[self.player play];
}
- (IBAction)otherMusic {
NSURL *url = [NSURL URLWithString:@"http://tsmusic128.tc.qq.com/37023937.mp3"];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
// SINGLE: 切換音樂(lè)
[self.player replaceCurrentItemWithPlayerItem:item];
}
/** 懶加載 */
- (AVPlayer *)player
{
if (_player == nil) {
// REMARKS: 創(chuàng)建播放器,通過(guò)playerWithPlayerItem方法創(chuàng)建音樂(lè)播放器,播放網(wǎng)絡(luò)音樂(lè)
NSURL *url = [NSURL URLWithString:@"http://cc.stream.qqmusic.qq.com/C100003j8IiV1X8Oaw.m4a?fromtag=52"];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
_player = [AVPlayer playerWithPlayerItem:item];
}
return _player;
}
2.播放網(wǎng)絡(luò)視頻(AVPlayer)
AVPlayer屬于AVFoundation框架
使用AVPlayer實(shí)現(xiàn)網(wǎng)絡(luò)視頻的播放
- AVPlayer播放網(wǎng)絡(luò)視頻
- 創(chuàng)建AVPlayerLayer,設(shè)置AVPlayerLayer的frame,并添加到控制器view的Layer上,實(shí)現(xiàn)視頻播放
- 使用playerLayerWithPlayer方法創(chuàng)建AVPlayerLayer
- 創(chuàng)建AVPlayerLayer,設(shè)置AVPlayerLayer的frame,并添加到控制器view的Layer上,實(shí)現(xiàn)視頻播放
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
playerLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
[self.view.layer addSublayer:playerLayer];
- AVPlayer播放網(wǎng)絡(luò)視頻示例代碼
// REMARKS: 通過(guò)AVPlayer實(shí)現(xiàn)視頻播放
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.player play];
}
/** 懶加載AVPlayer */
- (AVPlayer *)player
{
if (_player == nil) {
// 創(chuàng)建AVPlayer
NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
_player = [AVPlayer playerWithPlayerItem:item];
// SINGLE: 使用playerLayerWithPlayer方法創(chuàng)建AVPlayerLayer,并設(shè)置AVPlayerLayer的frame,添加到控制器的view的Layer上,實(shí)現(xiàn)視頻播放
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
playerLayer.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
[self.view.layer addSublayer:playerLayer];
}
return _player;
}
3.MPMoviePlayerController視頻播放器控制器實(shí)現(xiàn)播放視頻
MPMoviePlayerController視頻播放器控制器屬于MediaPlayer框架
MPMoviePlayerController播放視頻
- 使用MPMoviePlayerController播放視頻步驟
- 創(chuàng)建MPMoviePlayerController視頻播放器
- 設(shè)置播放器view的frame
- 將播放器的view添加到當(dāng)前控制器的view上
- 設(shè)置全屏播放,橫豎屏切換有問(wèn)題
_playerController.controlStyle = MPMovieControlStyleFullscreen; - 播放視頻[self.playerController play];
- MPMoviePlayerController播放視頻示例代碼
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
/** MPMoviePlayerController視頻播放器控制器 */
@property (nonatomic, strong) MPMoviePlayerController *playerController;
@end
@implementation ViewController
// REMARKS: 使用MPMoviePlayerController播放視頻
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.playerController play];
}
- (MPMoviePlayerController *)playerController
{
if (_playerController == nil) {
NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"];
// 創(chuàng)建視頻播放器
_playerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
// 設(shè)置播放器view的frame
_playerController.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
// 將播放器的view添加到當(dāng)前控制器的view上
[self.view addSubview:_playerController.view];
// SINGLE: 設(shè)置全屏播放,橫豎屏切換有問(wèn)題
// _playerController.controlStyle = MPMovieControlStyleNone;
_playerController.controlStyle = MPMovieControlStyleFullscreen;
}
return _playerController;
}
@end
4.MPMoviePlayerViewController播放網(wǎng)絡(luò)視頻
MPMoviePlayerViewController屬于MediaPlayer框架
MPMoviePlayerViewController播放視頻
- MPMoviePlayerViewController播放視頻實(shí)現(xiàn)步驟
- MPMoviePlayerViewController繼承UIViewController,所以播放器可以直接用Modal方式展示
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
@property (nonatomic, strong) MPMoviePlayerViewController *moviePlayerController;
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self presentViewController:self.moviePlayerController animated:YES completion:nil];
}
// REMARKS: 使用MPMoviePlayerViewController播放視頻
- (MPMoviePlayerViewController *)moviePlayerController
{
if (_moviePlayerController == nil) {
NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"];
_moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
}
return _moviePlayerController;
}
@end
5.使用AVPlayerViewController播放網(wǎng)絡(luò)視頻
AVPlayerViewController屬于AVKit框架
使用AVKit/AVKit框架中的AVPlayerViewController播放視頻
- 示例代碼
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
@interface ViewController ()
@property (nonatomic, strong) AVPlayerViewController *playerViewController;
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self presentViewController:self.playerViewController animated:YES completion:nil];
}
// REMARKS: 使用AVKit/AVKit框架中的AVPlayerViewController播放視頻
- (AVPlayerViewController *)playerViewController
{
if (_playerViewController == nil) {
NSURL *url = [NSURL URLWithString:@"http://v1.mukewang.com/19954d8f-e2c2-4c0a-b8c1-a4c826b5ca8b/L.mp4"];
AVPlayer *player = [AVPlayer playerWithURL:url];
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.player = player;
}
return _playerViewController;
}
@end
6.Vitamio SDK的集成
Vitamio SDK功能介紹
-
功能介紹
- 1.可手動(dòng)切換全半屏狀態(tài)
- 2.切換播放速率
- 3.切換視頻
- 4.支持橫豎屏
- 5.支持本地與網(wǎng)絡(luò)視頻
-
Vitamio SDK的github鏈接
7.SizeClasses的簡(jiǎn)單使用
屏幕適配
- 1.為什么蘋(píng)果推出SizeClasses
- iPhone3gs-4s : frame直接寫(xiě)死
- iPad : autoresizing—>根據(jù)父控件frame發(fā)生改變,子控件跟著一起改變
- iPhone5-iPhone5s : autolayout —>自動(dòng)布局
- iPhone6和iPhone6p : size Classes—>發(fā)現(xiàn)屏幕變的太多樣化,界面大統(tǒng)一
SizeClasses簡(jiǎn)述
-
SizeClasses概念
- 僅僅是對(duì)屏幕進(jìn)行了分類, 真正排布UI元素還得使用autolayout
- 不再有橫豎屏的概念, 只有屏幕尺寸的概念
- 不再有具體尺寸的概念, 只有抽象尺寸的概念
-
把寬度和高度各分為3種情況
- Compact : 緊湊(小)
- Any : 任意
- Regular : 寬松(大)
SizeClasses可分為9類
-
練習(xí)SizeClasses
- 練習(xí)1:UIButton手機(jī)橫屏的時(shí)候顯示,豎屏的時(shí)候不顯示
- 練習(xí)2:橫屏的時(shí)候在左上角,豎屏的時(shí)候,在右下角
- 練習(xí)3:iPad當(dāng)中也顯示在中間
- 練習(xí)4:圖片的顯示 : 一般情況下顯示一張圖片,iPad顯示特有的圖片
- 練習(xí)5:Label的顯示
- 練習(xí)6:約束沖突
-
符號(hào)代表
減號(hào)- 表示 Compact
乘號(hào) 表示 Any*
加號(hào)+ 表示 Regular- storyboard SizeClasses截圖
-
繼承性
w:Compact h:Compact 繼承 (w:Any h:Compact , w:Compact h:Any , w:Any h:Any)
w:Regular h:Compact 繼承 (w:Any h:Compact , w:Regular h:Any , w:Any h:Any)
w:Compact h:Regular 繼承 (w:Any h:Regular , w:Compact h:Any , w:Any h:Any)
w:Regular h:Regular 繼承 (w:Any h:Regular , w:Regular h:Any , w:Any h:Any) -
設(shè)備對(duì)應(yīng)屏幕
iPhone4S,iPhone5/5s,iPhone6
豎屏:(w:Compact h:Regular)
橫屏:(w:Compact h:Compact)
iPhone6 Plus
豎屏:(w:Compact h:Regular)
橫屏:(w:Regular h:Compact)
iPad
豎屏:(w:Regular h:Regular)
橫屏:(w:Regular h:Regular)
Apple Watch(猜測(cè))
豎屏:(w:Compact h:Compact)
橫屏:(w:Compact h:Compact) -
sizeclass和autolayout的作用
sizeclass:僅僅是對(duì)屏幕進(jìn)行了分類
autolayout:對(duì)屏幕中各種元素進(jìn)行約束(位置\尺寸)