Skip to content

Commit

Permalink
1.add loading indicator when buffering.
Browse files Browse the repository at this point in the history
2.add check the free size of device is usable before save into the device
3. thanks for @EverMe (https://github.com/EverMe) pull some good ideas.
  • Loading branch information
dito010 committed Dec 1, 2016
1 parent 6b3312f commit 65cd1ad
Show file tree
Hide file tree
Showing 28 changed files with 476 additions and 250 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file modified Images/JPVideoPlayer.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Images/JPVideoPlayer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion JPVideoPlayer/JPCacheManager.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#import <Foundation/Foundation.h>

typedef void(^JPCacheQueryCompletedBlock)(NSUInteger);
typedef void(^JPCacheQueryCompletedBlock)(unsigned long long);

@interface JPCacheManager : NSObject

Expand All @@ -36,4 +36,10 @@ typedef void(^JPCacheQueryCompletedBlock)(NSUInteger);
*/
+(void)getSize:(JPCacheQueryCompletedBlock)completedOperation;

/**
* Get the free size of device.
* 获取磁盘剩余存储空间
*/
+(unsigned long long )getDiskFreeSize;

@end
14 changes: 13 additions & 1 deletion JPVideoPlayer/JPCacheManager.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
#import "JPCacheManager.h"
#import "JPVideoCachePathTool.h"
#import "JPVideoURLAssetResourceLoader.h"
#include <sys/param.h>
#include <sys/mount.h>

@implementation JPCacheManager

+(void)clearVideoCacheForUrl:(NSURL *)url{

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *savePa = [JPVideoCachePathTool fileSavePath];
NSString *suggestFileName = [[url absoluteString]lastPathComponent];
NSString *suggestFileName = [JPVideoCachePathTool suggestFileNameWithURL:url];
savePa = [savePa stringByAppendingPathComponent:suggestFileName];
if ([fileManager fileExistsAtPath:savePa]) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
Expand Down Expand Up @@ -82,4 +85,13 @@ +(void)getSizeWithDirectoryPath:(NSArray *)directoryPathArr completion:(void(^)(
});
}

+ (unsigned long long)getDiskFreeSize{
struct statfs buf;
unsigned long long freespace = -1;
if(statfs("/var", &buf) >= 0){
freespace = (long long)(buf.f_bsize * buf.f_bfree);
}
return freespace;
}

@end
28 changes: 14 additions & 14 deletions JPVideoPlayer/JPDownloadManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,62 +54,62 @@
@interface JPDownloadManager : NSObject

/**
* The url of network file
* The url of network file.
* 要下载的文件的URL
*/
@property (nonatomic, strong, readonly) NSURL *url;

/**
* The value of start download position
* The value of start download position.
* 下载位置的偏移量
*/
@property (nonatomic, readonly) NSUInteger offset;

/**
* The total length of file
* The total length of file.
* 文件总长度
*/
@property (nonatomic, readonly) NSUInteger fileLength;

/**
* The current length of downloaded file
* The current length of downloaded file.
* 当前下载了的文件的位置
*/
@property (nonatomic, readonly) NSUInteger downLoadingOffset;

/**
* The mimeType of the downloading file
* The mimeType of the downloading file.
* mineType 类型
*/
@property (nonatomic, strong, readonly) NSString *mimeType;

/**
* Query is finished download
* Query is finished download.
* 查询是否已经下载完成
*/
@property (nonatomic, assign)BOOL isFinishLoad;

/**
* To be the delegate, It can pass the statu of download by Delegate-Method
* @see JPDownloadManagerDelegate
* To be the delegate, It can pass the statu of download by Delegate-Method.
* @see JPDownloadManagerDelegate.
* 成为代理, 就能获得下载状态
*/
@property(nonatomic, weak)id<JPDownloadManagerDelegate> delegate;


/**
* It be used to save data as temporary file when requesting data from network
* It also can auto move temporary file to the path you assigned when the temporary file is a complete file (mean that the length of temporary file is equal to the file in network) after request finished or canceled
* And it will delete the temporary file if the temporary file is not a complete file after request finish or cancel
* It be used to save data as temporary file when requesting data from network.
* It also can auto move temporary file to the path you assigned when the temporary file is a complete file (mean that the length of temporary file is equal to the file in network) after request finished or canceled.
* And it will delete the temporary file if the temporary file is not a complete file after request finish or cancel.
* 传递要下载的文件的URL和下载初始偏移量, 这个方法功能是从网络请求数据,并把数据保存到本地的一个临时文件.
* 当网络请求结束或取消的时候,如果数据完整,则把数据缓存到指定的路径,不完整就删除
* @param url The url of network file
* @param offset The value of start download position, it can be 0
* @param url The url of network file.
* @param offset The value of start download position, it can be 0.
*/
- (void)setUrl:(NSURL *)url offset:(long long)offset;

/**
* Cancel current download task
* Cancel current download task.
* 取消当前下载进程
*/
- (void)invalidateAndCancel;
Expand Down
27 changes: 22 additions & 5 deletions JPVideoPlayer/JPDownloadManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "JPDownloadManager.h"
#import "JPVideoCachePathTool.h"
#import "JPCacheManager.h"

@interface JPDownloadManager()<NSURLSessionDataDelegate>

Expand Down Expand Up @@ -49,9 +50,7 @@ - (void)setUrl:(NSURL *)url offset:(long long)offset{
_curOffset = offset;
_downLoadingOffset = 0;

NSString *urlString = [url absoluteString];
self.suggestFileName = [urlString lastPathComponent];

self.suggestFileName = [JPVideoCachePathTool suggestFileNameWithURL:url];
[self startLoading];
}

Expand All @@ -67,7 +66,7 @@ -(void)invalidateAndCancel{
// 接收到服务器响应的时候
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler{

NSLog(@"ReceiveResponse, 开始下载");
// NSLog(@"ReceiveResponse, 开始下载");
_isFinishLoad = NO;
_mimeType = @"video/mp4";

Expand All @@ -91,6 +90,13 @@ -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataT
}
self.fileLength = fileLength;

// Compare DiskFreeSize and fileLength.
// 剩余空间与请求长度的判断
if (![self checkDiskFreeSize:fileLength]) {
completionHandler(NSURLSessionResponseCancel);
return;
}

if ([self.delegate respondsToSelector:@selector(manager:didReceiveVideoLength:mimeType:)]) {
[self.delegate manager:self didReceiveVideoLength:self.fileLength mimeType:self.mimeType];
}
Expand All @@ -115,7 +121,6 @@ -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataT
// For Test
// NSLog(@"loading ... 正在下载");
// NSLog(@"Download progress --- %0.2lf", 1.0 * _downLoadingOffset / self.fileLength);
// NSLog(@"DownloadManagerInstance %@", self);

if ([self.delegate respondsToSelector:@selector(manager:didReceiveData:downloadOffset:tempFilePath:)]) {
[self.delegate manager:self didReceiveData:data downloadOffset:_downLoadingOffset tempFilePath:_tempPath];
Expand Down Expand Up @@ -253,4 +258,16 @@ -(void)startLoading{
[dataTask resume];
}

- (BOOL)checkDiskFreeSize:(NSUInteger)length{

unsigned long long freeDiskSize = [JPCacheManager getDiskFreeSize];

if (freeDiskSize < length) {
// [AlertBox showMessage:@"手机存储空间不足,请清理后再试." hideAfter:2];
return NO;
}

return YES;
}

@end
6 changes: 6 additions & 0 deletions JPVideoPlayer/JPVideoCachePathTool.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ static NSString *jp_savePath = @"/JPVideoPlayer_save"; // complete file(完成
*/
+(NSString *)fileSavePath;

/**
* cache file Name
* 缓存的文件名字
*/
+(NSString *)suggestFileNameWithURL:(NSURL*)url;

@end
12 changes: 8 additions & 4 deletions JPVideoPlayer/JPVideoCachePathTool.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@

@implementation JPVideoCachePathTool

// Combine temporary file path
// Combine temporary file path.
// 拼接临时文件缓存存储路径
+(NSString *)fileCachePath{
return [self getFilePathWithAppendingString:jp_tempPath];
}


// Combine complete file path
// Combine complete file path.
// 拼接完整文件存储路径
+(NSString *)fileSavePath{
return [self getFilePathWithAppendingString:jp_savePath];
Expand All @@ -27,7 +26,7 @@ +(NSString *)getFilePathWithAppendingString:(NSString *)apdStr{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject stringByAppendingString:apdStr];

// Make folder
// Make folder.
// 创建文件夹
if (![fileManager fileExistsAtPath:path]) {
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
Expand All @@ -36,5 +35,10 @@ +(NSString *)getFilePathWithAppendingString:(NSString *)apdStr{
return path;
}

// cache file Name.
// 缓存的文件名字
+(NSString *)suggestFileNameWithURL:(NSURL*)url{
return [url.absoluteString.lastPathComponent componentsSeparatedByString:@"?"].firstObject;
}

@end
38 changes: 35 additions & 3 deletions JPVideoPlayer/JPVideoPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,24 @@
#import <UIKit/UIKit.h>
#import "JPCacheManager.h"


/**
* Use custom loading need implement this protocol.
* 使用自定义的loading时, 需实现此协议
*/
@protocol JPVideoPlayerLoadingDelegate<NSObject>

@required
- (void)startAnimating;
- (void)stopAnimating;

@end


@interface JPVideoPlayer : NSObject

/**
* Singleton
* Singleton.
* 单例
*/
+ (instancetype)sharedInstance;
Expand All @@ -56,14 +70,25 @@
*/
- (void)playWithUrl:(NSURL *)url showView:(UIView *)showView;

/**
* Default is YES.
*/
@property (nonatomic, assign) BOOL showActivityWhenLoading;

/**
* The loading view before video play.
* 视频加载视图, 默认为系统UIActivityIndicatorView
* Default is UIActivityIndicatorView
*/
@property (nonatomic,strong) UIView<JPVideoPlayerLoadingDelegate> *loadingView;

/**
* Default is YES
* Default is YES.
*/
@property (nonatomic, assign) BOOL stopWhenAppDidEnterBackground;

/**
* mute
* Mute.
* 静音
*/
@property(nonatomic, assign)BOOL mute;
Expand All @@ -72,6 +97,12 @@
-(void)pause;
-(void)stop;

/**
* The maximum disk cache. 1GB default, automatic clear all cache when the size of cache > 1GB.
* 最大磁盘缓存. 默认为 1G, 超过 1G 将自动清空所有视频磁盘缓存.
*/
@property(nonatomic, assign)unsigned long long maxCacheSize;

/**
* Clear video cache for the given url asynchronously.
* 清除指定URL的缓存视频文件(异步).
Expand All @@ -92,3 +123,4 @@
-(void)getSize:(JPCacheQueryCompletedBlock)completedOperation;

@end

Loading

0 comments on commit 65cd1ad

Please sign in to comment.