【移動應(yīng)用開發(fā)技術(shù)】iOS視頻播放方式有哪些_第1頁
【移動應(yīng)用開發(fā)技術(shù)】iOS視頻播放方式有哪些_第2頁
【移動應(yīng)用開發(fā)技術(shù)】iOS視頻播放方式有哪些_第3頁
【移動應(yīng)用開發(fā)技術(shù)】iOS視頻播放方式有哪些_第4頁
【移動應(yīng)用開發(fā)技術(shù)】iOS視頻播放方式有哪些_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

付費(fèi)下載

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】iOS視頻播放方式有哪些

/upload/information/20200623/126/120462.gif//

初始化KRVideoPlayerController

-

(instancetype)initWithFrame:(CGRect)frame

{

self

=

[super

init];

if

(self)

{

self.view.frame

=

frame;

self.view.backgroundColor

=

[UIColor

blackColor];

self.controlStyle

=

MPMovieControlStyleNone;

[self.view

addSubview:self.videoControl];

self.videoControl.frame

=

self.view.bounds;

[self

configObserver];

[self

configControlAction];

}

return

self;

}

//

懶加載KRVideoPlayerControlView

-

(KRVideoPlayerControlView

*)videoControl

{

if

(!_videoControl)

{

_videoControl

=

[[KRVideoPlayerControlView

alloc]

init];

}

return

_videoControl;

}/upload/information/20200623/126/120463.png//

1、即將開始畫中畫

-

(void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController

*)playerViewController;

//

2、開始畫中畫

-

(void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController

*)playerViewController;

//

3、畫中畫失敗

-

(void)playerViewController:(AVPlayerViewController

*)playerViewController

failedToStartPictureInPictureWithError:(NSError

*)error;

//

4、即將結(jié)束畫中畫

-

(void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController

*)playerViewController;

//

5、結(jié)束畫中畫

-

(void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController

*)playerViewController;/upload/information/20200623/126/120464.gif-

(void)viewDidLoad

{

[super

viewDidLoad];

//

Do

any

additional

setup

after

loading

the

view.

self.view.backgroundColor

=

[UIColor

whiteColor];

self.avPlayerItem

=

[AVPlayerItem

playerItemWithURL:[NSURL

URLWithString:MovieURL]];

self.avPlayer

=

[[AVPlayer

alloc]initWithPlayerItem:self.avPlayerItem];

self.avPlayerLayer

=

[AVPlayerLayer

playerLayerWithPlayer:self.avPlayer];

self.avPlayerLayer.frame

=

CGRectMake(10,

100,

355,

200);

[self.view.layer

addSublayer:self.avPlayerLayer];

//

添加觀察者

[self

addObserverWithAVPlayerItem];

}

#pragma

mark

--

#pragma

mark

--

KVO

-(void)addObserverWithAVPlayerItem{

//狀態(tài)添加觀察者

[self.avPlayerItem

addObserver:self

forKeyPath:@"status"

options:(NSKeyValueObservingOptionNew)

context:nil];

//

緩存進(jìn)度添加觀察者

[self.avPlayerItem

addObserver:self

forKeyPath:@"loadedTimeRanges"

options:NSKeyValueObservingOptionNew

context:nil];

}

-(void)observeValueForKeyPath:(NSString

*)keyPath

ofObject:(id)object

change:(NSDictionary<NSKeyValueChangeKey,id>

*)change

context:(void

*)context{

AVPlayerItem

*

avplayeritem

=

(AVPlayerItem

*)object;

if

([keyPath

isEqualToString:@"status"])

{

AVPlayerStatus

status

=

[[change

objectForKey:@"new"]

intValue];

if

(status

==

AVPlayerStatusReadyToPlay)

{

NSLog(@"準(zhǔn)備好播放");

CMTime

duration

=

avplayeritem.duration;

NSLog(@"視頻總時長:%.2f",CMTimeGetSeconds(duration));

//

播放

[self.avPlayer

play];

}else

if

(status

==

AVPlayerStatusFailed){

NSLog(@"視頻準(zhǔn)備發(fā)生錯誤");

}else{

NSLog(@"位置錯誤");

}

}else

if

([keyPath

isEqualToString:@"loadedTimeRanges"]){

//

可以自定義緩存進(jìn)度

NSTimeInterval

timeInterval

=

[self

alreadyCacheVideoProgress];

NSLog(@"視頻已經(jīng)緩存的時長:%.2f",timeInterval);

}

}

#pragma

mark

--

#pragma

mark

--

alreadyCacheVideoProgress

-(NSTimeInterval)alreadyCacheVideoProgress{

//

先獲取到它的緩存的進(jìn)度

NSArray

*

cacheVideoTime

=

[self.avPlayerItem

loadedTimeRanges];

//

CMTimeRange

結(jié)構(gòu)體

start

duration

表示起始位置

持續(xù)時間

//

獲取緩沖區(qū)域

CMTimeRange

timeRange

=

[cacheVideoTime.firstObject

CMTimeRangeValue];

float

startSeconds

=

CMTimeGetSeconds(timeRange.start);

float

durationSeconds

=

CMTimeGetSeconds(timeRange.duration);

//

計算總緩沖時間

=

start

+

duration

NSTimeInterval

result

=

startSeconds

+

durationSeconds;

return

result;

}/*!

@typedef CMTime

@abstract Rational

time

value

represented

as

int64/int32.

*/

typedef

struct

{

CMTimeValue value; /*!

@field

value

The

value

of

the

CMTime.

value/timescale

=

seconds.

幀數(shù)

*/

CMTimeScale timescale; /*!

@field

timescale

The

timescale

of

the

CMTime.

value/timescale

=

seconds.幀率(影片每秒有幾幀)*/

CMTimeFlags

flags;

/*!

@field

flags

The

flags,

eg.

kCMTimeFlags_Valid,

kCMTimeFlags_PositiveInfinity,

etc.

*/

CMTimeEpoch

epoch;

/*!

@field

epoch

Differentiates

between

equal

timestamps

that

are

actually

different

because

of

looping,

multi-item

sequencing,

etc.

Will

be

used

during

comparison:

greater

epochs

happen

after

lesser

ones.

Additions/subtraction

is

only

possible

within

a

single

epoch,

however,

since

epoch

length

may

be

unknown/variable.

*/}

CMTime;CMTime

duration

=

avplayeritem.duration;

NSLog(@"視頻總時長:%.2f",CMTimeGetSeconds(duration));-

(id)addPeriodicTimeObserverForInterval:(CMTime)interval

queue:(nullable

dispatch_queue_t)queue

usingBlock:(void

(^)(CMTime

time))block;[self.avPlayer

addPeriodicTimeObserverForInterval:CMTimeMake(1,

10)

queue:dispatch_get_main_queue()

usingBlock:^(CMTime

time)

{

//

CMTime的timescale的定義幫助理解下面代碼

//

@field

timescale

The

timescale

of

the

CMTime.

value/timescale

=

seconds.

float

currentPlayTime

=

(double)self.avPlayerItem.currentTime.value/

self.avPlayerItem.currentTime.timescale;

NSLog(@"當(dāng)前播放進(jìn)度:%f",currentPlayTime);

}];/upload/information/20200623/126/120465.png/*

Note

that

NSNotifications

posted

by

AVPlayerItem

may

be

posted

on

a

different

thread

from

the

one

on

which

the

observer

was

registered.

*/

//

notifications

description

AVF_EXPORT

NSString

*const

AVPlayerItemTimeJumpedNotification

NS_AVAILABLE(10_7,

5_0); //

the

item's

current

time

has

changed

discontinuously

AVF_EXPORT

NSString

*const

AVPlayerItemDidPlayToEndTimeNotification

NS_AVAILABLE(10_7,

4_0);

//

item

has

played

to

its

end

time

AVF_EXPORT

NSString

*const

AVPlayerItemFailedToPlayToEndTimeNotification

NS_AVAILABLE(10_7,

4

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論