Returns a data object containing the data from the location specified by a given URL.
Declaration
+ (instancetype)dataWithContentsOfURL:(NSURL *)url;
Parameters
-
aURL
-
The URL from which to read data.
Return Value
A data object containing the data from the location specified by aURL
.
Returns nil
if the data object could not be created.
Discussion
Use this method to converting data://
URLs
to NSData objects. You can also use it to read short files synchronously. If you need to read potentially large files, use inputStreamWithURL:
to
open a stream, then read the file incrementally.
If you need to know the reason for failure, use dataWithContentsOfURL:options:error:
.
參考地址:https://developer.apple.com/documentation/foundation/nsdata/1547245-datawithcontentsofurl
沒怎麼研究視頻播放器,就在網上找了個,鏈接:https://github.com/835239104/KrVideoPlayerPlus
下載AFNetworking,
1.下載最新版的會報錯,沒有引入相關庫
2.AFSecurityPolicy.m中:註釋了一部分代碼
#pragma mark - 這裏改了 ------
#warning - 這裏改了 --------
//#if !TARGET_OS_IOS && !TARGET_OS_WATCH
//static NSData * AFSecKeyGetData(SecKeyRef key) {
// CFDataRef data = NULL;
//
// __Require_noErr_Quiet(SecItemExport(key, kSecFormatUnknown, kSecItemPemArmour, NULL, &data), _out);
//
// return (__bridge_transfer NSData *)data;
//
//_out:
// if (data) {
// CFRelease(data);
// }
//
// return nil;
//}
//#endif
static BOOL AFSecKeyIsEqualToKey(SecKeyRef key1, SecKeyRef key2) {
//#if TARGET_OS_IOS || TARGET_OS_WATCH
return [(__bridge id)key1 isEqual:(__bridge id)key2];
//#else
// return [AFSecKeyGetData(key1) isEqual:AFSecKeyGetData(key2)];
//#endif
}
3.實現邊下邊播(我用的是一邊在網上看一邊下載,所以會有點卡)
/**
* 下載文件
*/
- (void)downloadFileURL:(NSString *)aUrl savePath:(NSString *)aSavePath fileName:(NSString *)aFileName tag:(NSInteger)aTag
{
NSFileManager *fileManager = [NSFileManager defaultManager];
//檢查本地文件是否已存在
NSString *fileName = [NSString stringWithFormat:@"%@/%@", aSavePath, aFileName];
//檢查附件是否存在
if ([fileManager fileExistsAtPath:fileName]) {
[self addVideoPlayerWithURL:[NSURL fileURLWithPath:fileName]];
}else{
//創建附件存儲目錄
if (![fileManager fileExistsAtPath:aSavePath]) {
[fileManager createDirectoryAtPath:aSavePath withIntermediateDirectories:YES attributes:nil error:nil];
}
// [self addVideoPlayerWithURL:[NSURL fileURLWithPath:fileName]];
[self addVideoPlayerWithURL:[NSURL URLWithString:aUrl]];
//下載附件
NSURL *url = [[NSURL alloc] initWithString:aUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.inputStream = [NSInputStream inputStreamWithURL:url];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:fileName append:NO];
//下載進度控制
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"is download:%f", (float)totalBytesRead/totalBytesExpectedToRead);
}];
//已完成下載
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// NSData *audioData = [NSData dataWithContentsOfFile:fileName];
NSLog(@"完成下載");
//設置下載數據到res字典對象中並用代理返回下載數據NSData
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"下載失敗");
//下載失敗
}];
[operation start];
}
}
注意:從本度讀取路徑:[NSURL fileURLWithPath:fileName]
在網上讀取路徑:[NSURL URLWithString:aUrl]
Demo百度雲下載鏈接:http://pan.baidu.com/s/1c0bL9dQ
補充:iOS邊下邊播放 http://blog.csdn.net/zttjhm/article/details/38063605
iOS視頻壓縮:http://blog.csdn.net/lookyou111/article/details/25625775