音樂(lè)播放器

IOS9.0的改變

棄用MPMoviePlayerViewController(導(dǎo)入的是MediaPlayer框架)

使用AVPlayerViewController(導(dǎo)入的是AVKit框架)

AVPlayer 是專門用來(lái)播放音視頻的類

AVPlayer的一些常用屬性

play

pause

跳轉(zhuǎn)進(jìn)度 seekToTime:

currentItem 當(dāng)前播放的視頻元素

volume 調(diào)節(jié)音量

externalPlaybackVideoGravity 視頻顯示的播放樣式

AVLayerVideoGravityResizeAspect 普通的

AVLayerVideoGravityResizeAspectFill充滿的

currentTime獲得當(dāng)前時(shí)間 -> CMTime ->CMTimeGetSeconds()通過(guò)CMTime獲得當(dāng)前播放時(shí)間(單位是秒)

CMTime專門用來(lái)表示視頻的播放進(jìn)度的

value(進(jìn)度)

timeScale(幀率)

kCMTimeZero 表示初始進(jìn)度

seekToTime:可以跳轉(zhuǎn)到某一個(gè)進(jìn)度

AVPlayerItem 要播放的音視頻的對(duì)象

duration 是CMTime類型 總時(shí)長(zhǎng)

status 加載的狀態(tài)

AVPlayerItemStatusUnknown, 未知狀態(tài)

AVPlayerItemStatusReadyToPlay, 準(zhǔn)備播放狀態(tài)

AVPlayerItemStatusFailed? 失敗狀態(tài)

時(shí)間控制的一個(gè)類目

currentTime? 獲得當(dāng)前播放時(shí)間

forwardPlaybackEndTime? 跳到結(jié)束位置

reversePlaybackEndTime? 跳到開(kāi)始位置

seekToTime:

AVPlayerLayer 播放顯示音視頻的圖層界面

AVPlayerViewController? 視圖控制器 可以顯示視頻并且有調(diào)節(jié)控件

使用

AVPlayer直接播放

創(chuàng)建AVPlayerItem 視頻內(nèi)容相關(guān)

創(chuàng)建方式

1、playerItemWithURL:類方法 通過(guò)URL地址創(chuàng)建要播放的對(duì)象(可以播放本地的內(nèi)容也可以播放在線的內(nèi)容)

2、initWithURL:構(gòu)造方法

3、playerItemWithAsset:通過(guò)設(shè)備相冊(cè)里面的內(nèi)容創(chuàng)建一個(gè)對(duì)象

4、initWithAsset:

5、playerItemWithAsset:automaticallyLoadedAssetKeys自動(dòng)根據(jù)要求的Key去加載相冊(cè)里面的內(nèi)容

6、initWithAsset:automaticallyLoadedAssetKeys:

創(chuàng)建AVPlayer

<1>創(chuàng)建方式? 視頻操作相關(guān)

1、playerWithURL:根據(jù)URL去創(chuàng)建播放器 不需使用item2、initWithURL:3、playerWithPlayerItem:需要傳入一個(gè)視頻播放內(nèi)容4、initWithPlayerItem:5、獲得播放結(jié)束的狀態(tài)6、seekToTime跳轉(zhuǎn)到某個(gè)進(jìn)度7、CMTime:結(jié)構(gòu)體? value(進(jìn)度) timeScale(幀率)

創(chuàng)建AVPlayerLayer 添加到父視圖

<1>根據(jù)播放器去創(chuàng)建一個(gè)可以顯示視頻的圖層playerLayerWithPlayer:->類方法<2>設(shè)置它的位置frame是沒(méi)有動(dòng)畫效果的<3>把視頻圖層添加到父圖層

播放 播放器 play

控制器播放

1、創(chuàng)建AVPlayer

2、創(chuàng)建視頻播放視圖的控制器

3、將創(chuàng)建的AVPlayer賦值給控制器自帶的player

4、跳轉(zhuǎn)到控制器播放

注意:使用控制器播放需要再導(dǎo)入一個(gè)AVKit框架

代碼示例

#import"ViewController.h"#import#import@interfaceViewController(){AVPlayer*player;}@end@implementationViewController- (void)viewDidLoad {? ? [superviewDidLoad];UIButton*button = [UIButtonbuttonWithType:UIButtonTypeCustom];? ? button.frame=CGRectMake(100,100,120,100);? ? [button setTitle:@"AVPlayer播放"forState:UIControlStateNormal];? ? [button addTarget:selfaction:@selector(demo1) forControlEvents:UIControlEventTouchUpInside];? ? [self.viewaddSubview:button];UIButton*button1 = [UIButtonbuttonWithType:UIButtonTypeCustom];? ? button1.frame=CGRectMake(240,100,100,100);? ? [button1 setTitle:@"控制器播放"forState:UIControlStateNormal];? ? [button1 addTarget:selfaction:@selector(demo2) forControlEvents:UIControlEventTouchUpInside];? ? [self.viewaddSubview:button1];}#pragma mark ------AVPlayer播放-(void)demo1{/*

視頻播放需要AVPlayer、AVPlayerItem、AVPlayerLayer

三者的關(guān)系及作用:

AVPlayer(視頻播放器) 去播放 -> AVPlayerItem(視頻播放的元素) -> AVPlayerLayer(展示播放的視圖)

*///1、創(chuàng)建要播放的元素/*

本地的一個(gè)視頻

NSString *path = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"m4v"];

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:path]];

*/AVPlayerItem*playerItem = [AVPlayerItemplayerItemWithURL:[NSURLURLWithString:@"http://down.treney.com/mov/test.mp4"]];//2、創(chuàng)建播放器player = [AVPlayerplayerWithPlayerItem:playerItem];//3、創(chuàng)建視頻顯示的圖層AVPlayerLayer*showVodioLayer = [AVPlayerLayerplayerLayerWithPlayer:player];? ? showVodioLayer.frame=self.view.frame;? ? [self.view.layeraddSublayer:showVodioLayer];//4、播放視頻[player play];//獲得播放結(jié)束的狀態(tài) -> 通過(guò)發(fā)送通知的形式獲得 ->AVPlayerItemDidPlayToEndTimeNotification[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(itemDidPlayToEndTime:) name:AVPlayerItemDidPlayToEndTimeNotificationobject:nil];//只要可以獲得到當(dāng)前視頻元素準(zhǔn)備好的狀態(tài) 就可以得到總時(shí)長(zhǎng)//采取KVO的形式獲得視頻總時(shí)長(zhǎng)//通過(guò)監(jiān)視status 判斷是否準(zhǔn)備好 -> 獲得[playerItem addObserver:selfforKeyPath:@"status"options:NSKeyValueObservingOptionNewcontext:nil];}//當(dāng)status的值改變的時(shí)候會(huì)調(diào)用這個(gè)方法-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context{NSLog(@"%@",change[@"new"]);AVPlayerItemStatusstatus = [change[@"new"] integerValue];switch(status) {caseAVPlayerItemStatusUnknown: {NSLog(@"未知狀態(tài)");break;? ? ? ? }caseAVPlayerItemStatusReadyToPlay: {NSLog(@"視頻的總時(shí)長(zhǎng)%f", CMTimeGetSeconds(player.currentItem.duration));break;? ? ? ? }caseAVPlayerItemStatusFailed: {NSLog(@"加載失敗");break;? ? ? ? }? ? }}//快進(jìn)-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent*)event{//快進(jìn)//跳到某一個(gè)進(jìn)度的方法:seekToTime://得到當(dāng)前的時(shí)間 + 快進(jìn)的時(shí)間//獲得當(dāng)前播放的時(shí)間 (秒)Float64 cur =? CMTimeGetSeconds(player.currentTime);? ? cur ++;? ? [player seekToTime:CMTimeMake(cur,1)];}-(void)itemDidPlayToEndTime:(NSNotification*)not{NSLog(@"播放結(jié)束");? ? [player seekToTime:kCMTimeZero];}#pragma mark -----控制器播放-(void)demo2{//1、創(chuàng)建AVPlayer/*

本地視頻

NSURL *url = [[NSBundle mainBundle]URLForResource:@"IMG_9638.m4v" withExtension:nil];

AVPlayer *player = [AVPlayer playerWithURL:url];

*///網(wǎng)頁(yè)視頻AVPlayer*player1 = [AVPlayerplayerWithURL:[NSURLURLWithString:@"http://down.treney.com/mov/test.mp4"]];//2、創(chuàng)建視頻播放視圖的控制器AVPlayerViewController*playerVC = [[AVPlayerViewControlleralloc]init];//3、將創(chuàng)建的AVPlayer賦值給控制器自帶的playerplayerVC.player= player1;//4、跳轉(zhuǎn)到控制器播放[selfpresentViewController:playerVC animated:YEScompletion:nil];? ? [playerVC.playerplay];}@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容