iOS開發(fā)中AVPlayer的簡(jiǎn)單應(yīng)用
前言
在iOS開發(fā)中,播放視頻通常有兩種方式,一種是使用MPMoviePlayerController(需要導(dǎo)入MediaPlayer.Framework),還有一種是使用AVPlayer。關(guān)于這兩個(gè)類的區(qū)別簡(jiǎn)而言之就是MPMoviePlayerController使用更簡(jiǎn)單,功能不如AVPlayer強(qiáng)大,而AVPlayer使用稍微麻煩點(diǎn),不過功能更加強(qiáng)大。下面這篇文章主要介紹下AVPlayer的簡(jiǎn)單應(yīng)用,需要的朋友們一起來(lái)看看吧。
AVPlayer的簡(jiǎn)單應(yīng)用
1.引入系統(tǒng)框架
2.創(chuàng)建視頻的url
3.創(chuàng)建播放項(xiàng)目
4.初始化播放器
5.設(shè)置播放頁(yè)面
實(shí)例代碼如下:
//引入系統(tǒng)文件
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
@interface ViewController ()
/**
* 控制視頻播放的控件
*/
@property (weak, nonatomic) IBOutlet UISlider *progressSlider;
/**
* 聲明播放視頻的控件屬性[既可以播放視頻也可以播放音頻]
*/
@property (nonatomic,strong)AVPlayer *player;
/**
* 播放的總時(shí)長(zhǎng)
*/
@property (nonatomic,assign)CGFloat sumPlayOperation;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//設(shè)置播放的url
NSString *playString = @"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4";
NSURL *url = [NSURL URLWithString:playString];
//設(shè)置播放的項(xiàng)目
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
//初始化player對(duì)象
self.player = [[AVPlayer alloc] initWithPlayerItem:item];
//設(shè)置播放頁(yè)面
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_player];
//設(shè)置播放頁(yè)面的大小
layer.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300);
layer.backgroundColor = [UIColor cyanColor].CGColor;
//設(shè)置播放窗口和當(dāng)前視圖之間的比例顯示內(nèi)容
layer.videoGravity = AVLayerVideoGravityResizeAspect;
//添加播放視圖到self.view
[self.view.layer addSublayer:layer];
//設(shè)置播放進(jìn)度的默認(rèn)值
self.progressSlider.value = 0;
//設(shè)置播放的默認(rèn)音量值
self.player.volume = 1.0f;
}
#pragma mark - 開始按鈕響應(yīng)方法
- (IBAction)startPlayer:(id)sender {
[self.player play];
}
#pragma mark - 暫停按鈕響應(yīng)方法
- (IBAction)stopPlayer:(id)sender {
[self.player pause];
}
#pragma mark - 改變進(jìn)度
- (IBAction)changeProgress:(id)sender {
self.sumPlayOperation = self.player.currentItem.duration.value/self.player.currentItem.duration.timescale;
//CMTimeMake(a,b) a表示當(dāng)前時(shí)間,b表示每秒鐘有多少幀
[self.player seekToTime:CMTimeMakeWithSeconds(self.progressSlider.value*self.sumPlayOperation, self.player.currentItem.duration.timescale) completionHandler:^(BOOL finished) {
[self.player play];
}];
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位iOS開發(fā)者們能帶來(lái)一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- 淺析iOS中視頻播放的幾種方案
- iOS中視頻播放器的簡(jiǎn)單封裝詳解
- 詳解iOS應(yīng)用中播放本地視頻以及選取本地音頻的組件用法
- iOS 本地視頻和網(wǎng)絡(luò)視頻流播放實(shí)例代碼
- 詳解iOS App中調(diào)用AVAudioPlayer播放音頻文件的用法
- 講解iOS開發(fā)中對(duì)音效和音樂播放的簡(jiǎn)單實(shí)現(xiàn)
- iOS App中實(shí)現(xiàn)播放音效和音樂功能的簡(jiǎn)單示例
- iOS AVPlayer切換播放源實(shí)現(xiàn)連續(xù)播放和全屏切換的方法
- iOS利用AVPlayer播放網(wǎng)絡(luò)音樂的方法教程
- iOS開發(fā)中音頻視頻播放的簡(jiǎn)單實(shí)現(xiàn)方法
相關(guān)文章
IOS 出現(xiàn)問題POST網(wǎng)絡(luò)請(qǐng)求狀態(tài)code:500的解決方法
這篇文章主要介紹了IOS 出現(xiàn)問題POST網(wǎng)絡(luò)請(qǐng)求狀態(tài)code:500的解決方法的相關(guān)資料,需要的朋友可以參考下2017-02-02
ios開發(fā)Flutter構(gòu)建todo?list應(yīng)用
這篇文章主要為大家介紹了ios開發(fā)Flutter構(gòu)建todo?list應(yīng)用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
iOS開發(fā)中CAlayer層的屬性以及自定義層的方法
這篇文章主要介紹了iOS開發(fā)中CAlayer層的屬性以及自定義層的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11
iOS 縮小打包項(xiàng)目ipa大小的實(shí)現(xiàn)方法
下面小編就為大家分享一篇iOS 縮小打包項(xiàng)目ipa大小的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2017-12-12
iOS開發(fā)中UISwitch按鈕的使用方法簡(jiǎn)介
這篇文章主要介紹了iOS開發(fā)中UISwitch按鈕的使用方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11
iOS中Label實(shí)現(xiàn)顯示不同顏色與字體的方法
這篇文章主要給大家介紹了關(guān)于在iOS中Label實(shí)現(xiàn)顯示不同顏色與字體的相關(guān)資料,文中分別介紹了利用range或者文字兩種實(shí)現(xiàn)的方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-11-11

