眾所周知,AVFoundation給我們提供了一個(gè)非常大的可擴(kuò)展的框架,我們可以通過這個(gè)框架,對(duì)媒體資源進(jìn)行捕捉,組合,播放,處理等功能.這篇文章,則是主要介紹AVFoundation框架中,一個(gè)非常重要的類AVAsset.
1.何為AVAsset
AVAsset是一個(gè)抽象類和不可變類,定義了媒體資源混合呈現(xiàn)的方式.可以讓我們開發(fā)者在處理時(shí)基媒體提供了一種簡(jiǎn)單統(tǒng)一的方式,它并不是媒體資源,但是它可以作為時(shí)基媒體的容器.
2.創(chuàng)建方法
當(dāng)我們想為一個(gè)媒體資源創(chuàng)建AVAsset對(duì)象時(shí),可以通過URL對(duì)它進(jìn)行初始化,URL可以是本地資源也可以是一個(gè)網(wǎng)絡(luò)資源
<code>NSURLassetUrl = [NSURL URLWithString:@"1234"];
AVAssetasset = [AVAsset asetWithURL:assetUrl];</code>
通過對(duì)asset打印可以得知,當(dāng)它通過asetWithURL方法進(jìn)行創(chuàng)建時(shí),實(shí)際上是創(chuàng)建了它子類AVUrlAsset的一個(gè)實(shí)例,而AVAsset是一個(gè)抽象類,不能直接被實(shí)例化.
通過AVUrlAsset我們可以創(chuàng)建一個(gè)帶選項(xiàng)(optional)的asset,以提供更精確的時(shí)長(zhǎng)和計(jì)時(shí)信息
An instance of NSDictionary that contains keys for specifying options for the initialization of the AVURLAsset. See** AVURLAssetPreferPreciseDurationAndTimingKey** and AVURLAssetReferenceRestrictionsKey above.
- (instancetype)URLAssetWithURL:(NSURL*)URL options:(nullableNSDictionary *)options;<
NSURL*assetUrl = [NSURLURLWithString:@"1234"];
NSDictionary*optional =@{@"AVURLAssetPreferPreciseDurationAndTimingKey":@(YES)};
AVURLAsset*urlAsset = [[AVURLAssetalloc]initWithURL:assetUrloptions:optional];
3.屬性
AVAsset具有多種有用的方法和屬性,比如時(shí)長(zhǎng),創(chuàng)建日期和元數(shù)據(jù)等
/* Indicates the duration of the asset. If @"providesPreciseDurationAndTiming" is NO, a best-available estimate of the duration is returned. The degree of precision preferred for timing-related properties can be set at initialization time for assets initialized with URLs. See AVURLAssetPreferPreciseDurationAndTimingKey for AVURLAsset below.
*/
@property (nonatomic, readonly) CMTime duration;
/* indicates the natural rate at which the asset is to be played; often but not always 1.0
*/
@property (nonatomic, readonly) float preferredRate;
/*!
@property tracks
@abstract Provides the array of AVAssetTracks contained by the asset
*/
@property (nonatomic, readonly) NSArray<AVAssetTrack *> *tracks;
@property (nonatomic, readonly, nullable) AVMetadataItem *creationDate
其中duration的屬性是一個(gè)CMTime的結(jié)構(gòu)體
typedef struct
{
CMTimeValue value; /*! @field value The value of the CMTime. value/timescale = seconds. */
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;
獲取一個(gè)asset的時(shí)長(zhǎng)用value/timescale即可
4.異步載入
由于是當(dāng)創(chuàng)建時(shí),資源就是對(duì)媒體文件進(jìn)行處理.AVAsset使用一種高效的設(shè)計(jì)方法,延遲載入資源的屬性,什么時(shí)候使用,什么時(shí)候再加載.雖然這可以解決一些由于直接加載數(shù)據(jù)帶來的問題,但是訪問AVAsset的屬性如果耗時(shí)較長(zhǎng),而又發(fā)生在主線程,就會(huì)阻塞主線程,使界面無法響應(yīng).
所以AVAsset和AVAssetTrack都遵守了**
AVAsynchronousKeyValueLoading**協(xié)議
這是協(xié)議的兩個(gè)方法
- (AVKeyValueStatus)statusOfValueForKey:(NSString *)key error:(NSError * __nullable * __nullable)outError;
- (void)loadValuesAsynchronouslyForKeys:(NSArray<NSString *> *)keys completionHandler:(nullable void (^)(void))handler;
通過statusOfValueForKey方法查詢一個(gè)給定屬性的狀態(tài),該方法會(huì)返回一個(gè)枚舉類型的AVKeyValueStatus值,用于標(biāo)識(shí)狀態(tài)
通過調(diào)用loadValuesAsynchronouslyForKeys: completionHandler:方法,為其提供一個(gè)屬性的數(shù)組,當(dāng)資源出獄回應(yīng)請(qǐng)求狀態(tài)就會(huì)調(diào)用completionHandler代碼快
通過一個(gè)例子來進(jìn)行演示
AVAsset *asset = [AVAsset assetWithURL:assetUrl];
NSArray *keys = @[@"tracks",
@"playable",
@"duration"];
__weak typeof(asset) weakAsset = asset;
__weak typeof(self) weakSelf = self;
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
// check the keys
for (NSString *key in keys) {
NSError *error = nil;
AVKeyValueStatus keyStatus = [weakAsset statusOfValueForKey:key error:&error];
switch (keyStatus) {
case AVKeyValueStatusFailed:{
// failed
break;
}
case AVKeyValueStatusLoaded:{
// success
break;
}case AVKeyValueStatusCancelled:{
// cancelled
break;
}
default:
break;
}
}
// check playable
if (!weakAsset.playable) { // 不能播放
return;
}
});
}];
通常情況下只需監(jiān)聽tracks一個(gè)屬性即可,但是官方文檔上有這么一段話,所以在代碼里監(jiān)聽了三個(gè)屬性
The completion states of the keys you specify in keys
are not necessarily the same—some may be loaded, and others may have failed. You must check the status of each key individually using thestatusOfValue(forKey:error:)
method.
當(dāng)然我們亦可以通過蘋果提供的方法取消監(jiān)聽
/*!
@method cancelLoading
@abstract Cancels the loading of all values for all observers.
@discussion Deallocation or finalization of an instance of AVAsset will implicitly cancel loading if any loading requests are still outstanding.
*/
- (void)cancelLoading;