播放 视频
//
// VideoViewController.m
// VideoTest
//
// Created by lkk on 13-11-25.
// Copyright (c) 2013年 lkk. All rights reserved.
//
#import "VideoViewController.h"
#import "ASIHTTPRequest.h"
#import "AudioButton.h"
#import <MediaPlayer/MediaPlayer.h>
@interface VideoViewController ()
@end
@implementation VideoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
//注册 视频播放结束通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoFinished) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
return self;
}
- (void)viewDidLoad
{
// 5 横竖屏控制
AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.isFull = YES;
[super viewDidLoad];
self.view.backgroundColor = kGrayColor2;
// 修改1 创建播放按钮
AudioButton *musicBt = [[AudioButton alloc]initWithFrame:CGRectMake(135, 210, 50, 50)];
[musicBt addTarget:self action:@selector(videoPlay) forControlEvents:UIControlEventTouchUpInside];
[musicBt setTag:1];
[self.view addSubview:musicBt];
//播放
[self videoPlay];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
//播放
- (void)videoPlay{
NSString *webPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Temp"];
NSLog(@" web path :%@", webPath);
NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Cache"];
NSFileManager *fileManager=[NSFileManager defaultManager];
NSLog(@"cachepath :%@", cachePath);
if(![fileManager fileExistsAtPath:cachePath])
{
[fileManager createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:nil];
}
//添加 ID为名字 并加 .mp4 后缀
if ([fileManager fileExistsAtPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", _mp4_id]]]) {
// 修改2
//从缓存路径 读取 播放
MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", _mp4_id]]]];
[self presentMoviePlayerViewControllerAnimated:playerViewController];
videoRequest = nil;
NSLog(@"播放 本地");
}else{
ASIHTTPRequest *request=[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:_mp4_url]];
AudioButton *musicBt = (AudioButton *)[self.view viewWithTag:1];
[musicBt startSpin];
//下载完存储目录
[request setDownloadDestinationPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", _mp4_id]]];
//临时存储目录
[request setTemporaryFileDownloadPath:[webPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", _mp4_id]]];
[request setBytesReceivedBlock:^(unsigned long long size, unsigned long long total) {
[musicBt stopSpin];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setDouble:total forKey:@"file_length"];
Recordull += size;//Recordull全局变量,记录已下载的文件的大小
if (!isPlay&&Recordull > 400000) {
isPlay = !isPlay;
[self playVideo];
}
}];
//断点续载
[request setAllowResumeForFileDownloads:YES];
[request startAsynchronous];
videoRequest = request;
NSLog(@"播放 网络");
}
}
- (void)playVideo{
MPMoviePlayerViewController *playerViewController =[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://127.0.0.1:12345/%@.mp4", _mp4_id]]];
[self presentMoviePlayerViewControllerAnimated:playerViewController];
}
#pragma mark - 接收到播放结束通知执行
- (void)videoFinished{
if (videoRequest) {
isPlay = !isPlay;
[videoRequest clearDelegatesAndCancel];
videoRequest = nil;
}
// 修改三
[self.navigationController popViewControllerAnimated:YES];
// 4. 横竖屏控制
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.isFull =NO;
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val =UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
@end