System player (AVPlayer) does not support caching, and is more dependent on the network. I studied it again recently, and wrote a demo for convenient learning and research in the future. Functions: Support offline cache, offline playback, fast forward, disconnection and reconnection of video, automatically save data to local, preferentially use local data when replaying next time, etc. TTPlayerCache supports CocoaPods, which is convenient to use.
1. Usage:
pod ‘TTPlayerCache’
#import <TTPlayerCache.h>. // Convert the URL NSString *videoUrl = @ to an unrecognized URL"http://...."; videoUrl = TTResourceUrlFromOrigianllUrl(videoUrl); . . / / set AVPlayer play / / initializes the agent self. The resourceLoaderDelegate = [TTResourceLoaderDelegate new]; self.urlAsset = [AVURLAsset assetWithURL:self.videoURL]; [self.urlAsset.resourceLoadersetDelegate:self.resourceLoaderDelegate queue:TT_resourceLoader_delegate_queue()]; .Copy the code
2. Video playing process
Through the analysis of system player capture package: (only tested MP4 video format)
- AVPlayer will request the first time every time it playsbytes=0-2To obtain the total number of bytes of video, video type and other information
- Second request for full databytes=x-When data response may have different reaction after filling, request all data is requestsAllDataToEndOfResource = = YES.
- When the video is fast forward, the previous download task is cancelled, and **resourceLoader is also called if the fast forward area is not buffered: ShouldWaitForLoadingOfRequestedResource: method, it is appear requestsAllDataToEndOfResource loadingRequest = = NO/YES, and then continue to repeat. . When the video cache reaches a certain level, the system calls resourceLoader: didCancelLoadingRequest:** to cancel the download task.
3. Code analysis:
Less code, mainly TTResourceLoaderDelegate and TTResourceLoaderData these two classes
- TTResourceLoaderDelegate implementation AVAssetResourceLoaderDelegate method of two agents.
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader ShouldWaitForLoadingOfRequestedResource: (loadingRequest AVAssetResourceLoadingRequest *) {/ / to handle loadingRequest data backfill...returnYES; } - (void)resourceLoader:(AVAssetResourceLoader *)resourceLoader didCancelLoadingRequest:(AVAssetResourceLoadingRequest *)loadingRequest {// Cancel request processing... }Copy the code
- TTResourceLoaderData implements operations such as responding to loadingRequest data, caching, determining whether to continue requesting data (or using local data), and canceling the request task.
- TTResourceLoaderData Processing class for downloaded data.
- TTResourceLoaderCache Processing of locally cached data. You can obtain the total cache size, clear all caches, and delete a cache.
- TTPlayerCacheMacro defines constants and URL conversion methods such as Scheme of TTPlayerCache.
- TTReachability TTReachability is a prefix to Reachability, which manages the interruption of network processing during video playback.
– (void)handleAssetResourceLoadingRequest:(AVAssetResourceLoadingRequest *)loadingRequest {… } to loadingRequest processing, according to loadingRequest. DataRequest. RequestsAllDataToEndOfResource = = YES or not to separate (whether waiting for request all data), Encapsulate different NSURlRequests to determine whether to use local data. RequestsAllDataToEndOfResource to YES only one download task, on the cancelled prior to the start of a, and so on.
- Download taskWhen a response is received
- (void)TT_downloadTaskDataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response
, initializes a TTResourceLoaderData to save the total number of bytes and type of the video. TTResourceLoaderData will create an NSMutableData with the same length as the number of bytes of the video to populate the data. - Download taskWhen the data is received
- (void)TT_downloadTaskDataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
Call TTResourceLoaderData – >- (void)appendData:(NSData *)data taskId:(NSUInteger)taskIdentifier
Method to process data: Put data into_data
The correct position, adjustment_receivedDataPointArray
(Where the downloaded data is stored),loadingRequest.dataReqeust
Response data, look at the code. - Download taskDone or wrong
- (void)TT_downloadTaskTask:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
Call TTResourceLoaderData – >- (void)taskCompleteWithError:(NSError *)error taskId:(NSUInteger)taskIdentifier
rightDownload taskWhether to cancel, network error, normal completion handle respectively. - Called when the network is restored
- (void)reloadLoadingRequestWhenHasNetError
Refresh the player. Processing of the downloaded data is done in the TTResourceLoaderData class.
Key points:
LoadingRequest. DataRequest. RequestsAllDataToEndOfResource = = YES or NO actual location of the video data processing TTResourceLoaderData object said… Write for the first time, write more simple, wrong, bad place welcome correction, thank you!