#import
"ViewController.h"
#import "AFNetworking.h"
#import
@interface ViewController ()
@property ( strong , nonatomic ) IBOutlet UIView *progressView;
@property ( weak , nonatomic ) IBOutlet UIProgressView *progress;
@property ( weak , nonatomic ) IBOutlet UIButton *button;
@end
@implementation ViewController
- ( IBAction )ButtonTap:( id )sender {
// 使用 AFN 进行下载
}
- ( IBAction )ButtonTapa:( UIButton *)sender {
// 指定数据下载到那个文件当中
NSString *cachePath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
// 声明文件的路径
NSString *moviePath = [cachePath stringByAppendingPathComponent : @"mo.mp4" ];
// 判断 moviePath 路径下到底有没有文件
if ([[ NSFileManager defaultManager ] fileExistsAtPath :moviePath]) {
// 调用视频播放器播放本地视频
MPMoviePlayerViewController *moviePlayer = [[ MPMoviePlayerViewController alloc ] initWithContentURL :[ NSURL fileURLWithPath :moviePath]]; //NSURL fileURLWithPath:moviePath 本地文件的地址
moviePlayer. moviePlayer . scalingMode = MPMovieScalingModeAspectFit ;
moviePlayer. moviePlayer . controlStyle = MPMovieControlStyleEmbedded ;
moviePlayer. view . frame = CGRectMake ( 5 , 70 , self . view . frame . size . width - 10 , 200 );
[ self presentMoviePlayerViewControllerAnimated :moviePlayer];
return ;
}
// 使用 AFN 进行下载
// 创建操作对象 //http://live.dl.ltimg.net/livezip/download/5540385469401b10912f7a24?type=mp4
// -1 就是不设置超时时间 , 一般是 30 秒
NSURL *url = [ NSURL URLWithString : @"http://api.sina.cn/sinago/video_location.json?sf_i=4&video_id=138600506&fromsinago=1&postt=news_video_video_1&from=" ];
NSURLRequest *urlRequest = [ NSURLRequest requestWithURL :url cachePolicy : NSURLRequestUseProtocolCachePolicy timeoutInterval :- 1 ];
AFHTTPRequestOperation *operation = [[ AFHTTPRequestOperation alloc ] initWithRequest :urlRequest];
// 第一个参数 下载文件的路径
// 二 是否接着写入文件 , 用来做断点下载
operation. outputStream = [[ NSOutputStream alloc ] initToFileAtPath :moviePath append : NO ];
// 这个方法就是下载的过程 , 用来做进度条
// 第一个参数 本次下载了多少数据
// 二 一共下载了多少数据
// 三 这个文件或者视频 , 音频一共有多大
[operation setDownloadProgressBlock :^( NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
// 进度条显示进度
self . progress . progress = ( float )totalBytesRead/totalBytesExpectedToRead;
}];
// 下载完成后的方法
[operation setCompletionBlockWithSuccess :^( AFHTTPRequestOperation *operation, id responseObject) {
// 调用视频播放器播放本地视频
MPMoviePlayerViewController *moviePlayer = [[ MPMoviePlayerViewController alloc ] initWithContentURL :[ NSURL fileURLWithPath :moviePath]]; //NSURL fileURLWithPath:moviePath 本地文件的地址
[ self presentMoviePlayerViewControllerAnimated :moviePlayer];
} failure :^( AFHTTPRequestOperation *operation, NSError *error) {
NSLog ( @" 抱歉 , 下载失败 " );
}];
// 打开这个任务
[operation start ];
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view, typically from a nib.
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
#import "AFNetworking.h"
#import
@interface ViewController ()
@property ( strong , nonatomic ) IBOutlet UIView *progressView;
@property ( weak , nonatomic ) IBOutlet UIProgressView *progress;
@property ( weak , nonatomic ) IBOutlet UIButton *button;
@end
@implementation ViewController
- ( IBAction )ButtonTap:( id )sender {
// 使用 AFN 进行下载
}
- ( IBAction )ButtonTapa:( UIButton *)sender {
// 指定数据下载到那个文件当中
NSString *cachePath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
// 声明文件的路径
NSString *moviePath = [cachePath stringByAppendingPathComponent : @"mo.mp4" ];
// 判断 moviePath 路径下到底有没有文件
if ([[ NSFileManager defaultManager ] fileExistsAtPath :moviePath]) {
// 调用视频播放器播放本地视频
MPMoviePlayerViewController *moviePlayer = [[ MPMoviePlayerViewController alloc ] initWithContentURL :[ NSURL fileURLWithPath :moviePath]]; //NSURL fileURLWithPath:moviePath 本地文件的地址
moviePlayer. moviePlayer . scalingMode = MPMovieScalingModeAspectFit ;
moviePlayer. moviePlayer . controlStyle = MPMovieControlStyleEmbedded ;
moviePlayer. view . frame = CGRectMake ( 5 , 70 , self . view . frame . size . width - 10 , 200 );
[ self presentMoviePlayerViewControllerAnimated :moviePlayer];
return ;
}
// 使用 AFN 进行下载
// 创建操作对象 //http://live.dl.ltimg.net/livezip/download/5540385469401b10912f7a24?type=mp4
// -1 就是不设置超时时间 , 一般是 30 秒
NSURL *url = [ NSURL URLWithString : @"http://api.sina.cn/sinago/video_location.json?sf_i=4&video_id=138600506&fromsinago=1&postt=news_video_video_1&from=" ];
NSURLRequest *urlRequest = [ NSURLRequest requestWithURL :url cachePolicy : NSURLRequestUseProtocolCachePolicy timeoutInterval :- 1 ];
AFHTTPRequestOperation *operation = [[ AFHTTPRequestOperation alloc ] initWithRequest :urlRequest];
// 第一个参数 下载文件的路径
// 二 是否接着写入文件 , 用来做断点下载
operation. outputStream = [[ NSOutputStream alloc ] initToFileAtPath :moviePath append : NO ];
// 这个方法就是下载的过程 , 用来做进度条
// 第一个参数 本次下载了多少数据
// 二 一共下载了多少数据
// 三 这个文件或者视频 , 音频一共有多大
[operation setDownloadProgressBlock :^( NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
// 进度条显示进度
self . progress . progress = ( float )totalBytesRead/totalBytesExpectedToRead;
}];
// 下载完成后的方法
[operation setCompletionBlockWithSuccess :^( AFHTTPRequestOperation *operation, id responseObject) {
// 调用视频播放器播放本地视频
MPMoviePlayerViewController *moviePlayer = [[ MPMoviePlayerViewController alloc ] initWithContentURL :[ NSURL fileURLWithPath :moviePath]]; //NSURL fileURLWithPath:moviePath 本地文件的地址
[ self presentMoviePlayerViewControllerAnimated :moviePlayer];
} failure :^( AFHTTPRequestOperation *operation, NSError *error) {
NSLog ( @" 抱歉 , 下载失败 " );
}];
// 打开这个任务
[operation start ];
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view, typically from a nib.
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
@end