iOS-网络数据请求-视频异步下载播放

iOS-网络数据请求-视频异步下载播放
iOS-网络数据请求-视频异步下载播放
iOS-网络数据请求-视频异步下载播放
iOS-网络数据请求-视频异步下载播放
iOS-网络数据请求-视频异步下载播放
iOS-网络数据请求-视频异步下载播放
//
// ViewController.m
// 网络数据请求-视频下载播放
//
// Created by YIem on 16/3/1.
// Copyright © 2016年 YIem. All rights reserved.
//

import "ViewController.h"

// 系统播放器调用

import <AVFoundation/AVFoundation.h>

@interface ViewController ()<NSURLSessionDownloadDelegate>
// 视频
@property (nonatomic, retain) NSURLSessionDownloadTask *downTask;
// 下载进度条
@property (nonatomic, retain) UIProgressView *progressV;
@end

@implementation ViewController

  • (void)dealloc
    {

    [_progressV release];
    [_downTask release];
    [super dealloc];

    }

  • (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor greenColor];
    
    UIButton *movieButton = [UIButton buttonWithType:UIButtonTypeSystem];
    movieButton.frame = CGRectMake(50, 50, 80, 30);
    [movieButton setTitle:@"下载小电影" forState:UIControlStateNormal];
    [movieButton addTarget:self action:@selector(movieAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:movieButton];
    // 下载进度条
    self.progressV = [[UIProgressView alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width - 100, 30)];
    [self.view addSubview:_progressV];
    [_progressV release];
    
    // 暂停下载
    UIButton *pauseButton = [UIButton buttonWithType:UIButtonTypeSystem];
    pauseButton.frame = CGRectMake(50, 150, 100, 30);
    [pauseButton setTitle:@"暂停下载" forState:UIControlStateNormal];
    [pauseButton addTarget:self action:@selector(pauseAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pauseButton];
    // 继续下载
    UIButton *continueButton = [UIButton buttonWithType:UIButtonTypeSystem];
    continueButton.frame = CGRectMake(200, 150, 100, 30);
    [continueButton setTitle:@"继续下载" forState:UIControlStateNormal];
    [continueButton addTarget:self action:@selector(continueAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:continueButton];
    

}
// 暂停下载

  • (void)pauseAction
    {
if (NSURLSessionTaskStateRunning == self.downTask.state) {
    [self.downTask suspend];
}

}
// 继续下载

  • (void)continueAction
    {
    if (NSURLSessionTaskStateSuspended == self.downTask.state) {

    [self.downTask resume];

    }
    }

// 播放

  • (void)playWithFilePath:(NSString *)filePath
    {

    NSURL *url = [NSURL fileURLWithPath:filePath];
    AVPlayer *player = [AVPlayer playerWithURL:url];
    // 用来显示视频
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.frame = CGRectMake(50, 200, self.view.frame.size.width - 100, 300) ;
    
    // 添加显示
    [self.view.layer addSublayer:playerLayer];
    // 开始播放
    [player play];

    }

// 视频下载

  • (void)URLSession:(NSURLSession )session downloadTask:(NSURLSessionDownloadTask )downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
    {

    NSLog(@"下载过程中会多次调用, 每次下载不一定多大的数据");
    NSLog(@"本次下载大小: %.2fKB--已经下载大小: %.2fKB--总大小: %.2fKB", bytesWritten / 1024.0, totalBytesWritten / 1024.0,  totalBytesExpectedToWrite / 1024.0);
    self.progressV.progress = (CGFloat)totalBytesWritten / totalBytesExpectedToWrite;

    }

  • (void)URLSession:(NSURLSession )session downloadTask:(NSURLSessionDownloadTask )downloadTask didFinishDownloadingToURL:(NSURL *)location
    {

    NSLog(@"下载结束后调用");
    // 获取本地对应位置的路径
    NSString *cach = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    // 拼接文件的名字(系统默认)
    NSString *file = [cach stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
    NSFileManager *fileMan = [NSFileManager defaultManager];
    // 将下载的数据右临时文件移到本地路径
    [fileMan moveItemAtPath:location.path toPath:file error:nil];
    // 调用视频处理方法播放视频
    [self playWithFilePath:file];

    }

  • (void)movieAction
    {

    NSURL *url = [NSURL URLWithString:@"http://hc25.aipai.com/user/656/20448656/6167672/card/25033081/card.mp4?l=a"];
    NSURLRequest *requset = [NSURLRequest requestWithURL:url];
    // 参数1: 默认配置
    // 参数2: 代理人 //协议 <NSURLSessionDownloadDelegate>
    // 参数3: 再哪个线程内执行(当前选择主线程)
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    // 创建下载任务
    self.downTask = [session downloadTaskWithRequest:requset];
    // 开始任务
    [self.downTask resume];

    }

  • (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

    }

@end