视频播放

//

//  VideoViewController.m

//  LIBAOZHENG0826

//

//  Created by 张艳锋 on 15/8/30.

//  Copyright (c) 2015年 张艳锋. All rights reserved.

//

 

#import "VideoViewController.h"

 

@interface VideoViewController ()

{

    AVPlayer *player;

    UISlider *_slider;

    UILabel *timeLabel;

    AVPlayerItem *playeritem;

    UISlider *volumeSlider;

 

}

@property (weak, nonatomic) IBOutlet UIButton *doJumpbutton;

@property (weak, nonatomic) IBOutlet UIButton *doBackbutton;

- (IBAction)doBackButton:(UIButton *)sender ;

- (IBAction)doJump:(id)sender;

 

 

@end

 

@implementation VideoViewController

static int z=0;

- (void)viewDidLoad {

    [super viewDidLoad];

    NSLog(@"FWERW");

    NSString *urlPath=[[NSBundle mainBundle]pathForResource:@"hhh.mp4" ofType:nil];

    

    NSURL *url=[[NSURL alloc]initFileURLWithPath:urlPath];

    

    //AVURLAsset继承自AVAset,表示媒体的抽象类

    AVURLAsset *movieAsset=[[AVURLAsset alloc]initWithURL:url options:nil];

    //资源管理对象以及演示设置(资源缓冲状态)

    playeritem=[AVPlayerItem playerItemWithAsset:movieAsset];

    

    //添加通知

    [playeritem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:NULL];

    

    //播放器

    player=[AVPlayer playerWithPlayerItem:playeritem];

    [player setAllowsExternalPlayback:YES];

    AVPlayerLayer *layer=[AVPlayerLayer playerLayerWithPlayer:player];

//投影规格

    layer.frame=CGRectMake(58, 40, 450,240);

//

    layer.videoGravity=AVLayerVideoGravityResizeAspect;

    [self.view.layer addSublayer:layer];

    

    

    

    

    

    _slider=[[UISlider alloc]initWithFrame: CGRectMake(58, 300, 450, 20)];

    

    _slider.backgroundColor=[UIColor clearColor];

    _slider.value=0;

    _slider.minimumValue=0;

    _slider.maximumValue=1;

    [_slider setMaximumTrackImage:[UIImage imageNamed:@"播放进度条"] forState:UIControlStateNormal];

    [_slider setMinimumTrackImage:[UIImage imageNamed:@"播放进度条2"] forState:UIControlStateNormal];

    [_slider setThumbImage:[UIImage imageNamed:@"进度滑块"] forState:UIControlStateNormal];

    [_slider setThumbImage:[UIImage imageNamed:@"进度滑块"] forState:UIControlStateHighlighted];

    //注意这里要加UIControlStateHightlighted的状态,否则当拖动滑块时滑块将变成原生的控件

    [_slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];

    [_slider addTarget:self action:@selector(sliderDragUp:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_slider];

    

    

}

 

-(void)viewWillAppear:(BOOL)animated{

    

    

    

}

//返回按钮事件

- (IBAction)doBackButton:(UIButton *)sender {

    //返回上一视图

    [player pause];

//返回上一视图控制器控制的视图

    [self dismissViewControllerAnimated:YES completion:nil];

    

}

 

- (IBAction)doJump:(id)sender {

    //跳向指定视图

    //方法 1.根据指定的类 创建视图控制器    2.借用presentViewController方法跳向指定视图

  ChartsViewController *chartsVC=[[ChartsViewController   alloc]initWithNibName:@"ChartsViewController" bundle:nil];

    [self presentViewController:chartsVC animated:YES completion:nil];

    

}

 //移除通知,防止跳出或退出此视图时程序崩溃

-(void)dealloc{

    [playeritem removeObserver:self forKeyPath:@"status" context:NULL];

}

 

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{

    if ([keyPath isEqualToString:@"status"]) {

        if (AVPlayerItemStatusReadyToPlay == player.currentItem.status) {

            [player play];

            //添加计时器。

            [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showMovieTime:) userInfo:nil repeats:YES];

        }

    }

}

 

//返回视频的总时间

-(NSTimeInterval)playerDuration

{

    AVPlayerItem *item=player.currentItem;//获取资源管理者

    if (item.status == AVPlayerItemStatusReadyToPlay) {

        return CMTimeGetSeconds(player.currentItem.duration);

    }

    else{

        return (CMTimeGetSeconds(kCMTimeInvalid));

        

    }

}

 

//返回当前播放时间

-(NSTimeInterval)playerCurrentime

{

    AVPlayerItem *item=player.currentItem;//获取资源管理者

    if (item.status == AVPlayerItemStatusReadyToPlay) {

        if (CMTimeGetSeconds(player.currentItem.currentTime)==CMTimeGetSeconds(player.currentItem.duration)) {

            NSLog(@"end");

        }

        return CMTimeGetSeconds(player.currentItem.currentTime);

    }

    else{

        return (CMTimeGetSeconds(kCMTimeInvalid));

    }

}

//显示播放时间 

-(void)showMovieTime:(NSTimer *)timer{

    float duration=[self playerDuration];

    float current=[self playerCurrentime];

    float sliderNum=current/duration;

    NSLog(@"%f",sliderNum);

    _slider.value=sliderNum;

    

    int a=(int)current/60;

    int b=(int)current%60;

    int c=(int)duration/60;

    int d=(int)duration%60;

    

    NSString *timeString=[NSString stringWithFormat:@"%02d:%02d/%02d:%02d",a,b,c,d];

    timeLabel.text=timeString;

    

    _slider.value=current/duration;

    if (current>=duration) {

        [timer invalidate];

        [player pause];

    }

}

 

 

-(void)sliderDragUp:(UISlider *)slider1

{

    NSLog(@"slider:11111");

}

 //改变滑动条值

-(void)sliderValueChanged:(UISlider *)slider2

{

        float sliderValue=slider2.value;

        float allTime=[self playerDuration];

        

        CMTime changeTime=CMTimeMake(sliderValue*allTime, 1);

        [player.currentItem seekToTime:changeTime];

}

//点击按钮隐藏,点击按钮显现 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    if (z==0) {

        self.view.backgroundColor=[UIColor blackColor];

        _doBackbutton.hidden=YES;

        _doJumpbutton.hidden=YES;

        _slider.hidden=YES;

        z=1;

    }

    else {

        

        self.view.backgroundColor=[UIColor whiteColor];

        _doBackbutton.hidden=NO;

        _doJumpbutton.hidden=NO;

        _slider.hidden=NO;

 

        z=0;

 

    }

}

 

 

 

 

 

 

@end

posted @ 2015-09-06 16:59  OIMMZC  阅读(278)  评论(0编辑  收藏  举报