播放 视频

//

//  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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值