//
//  AVPlayerNetViewController.m
//  PodsTest
//
//  Created by ZhuYi on 16/4/29.
//  Copyright © 2016年 DDP. All rights reserved.
//

#import "AVPlayerNetViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface AVPlayerNetViewController (){
    AVPlayer *player;//播放器对象
    UIView *container; //播放器容器
    UIButton *playOrPause; //播放/暂停按钮
    UIProgressView *progress;//播放进度
    AVPlayerItem *playerItem;//播放项
}
@end

@implementation AVPlayerNetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];

    [self container];
    [self progressView];
    [self setUpPlayer];
}
- (void)container{
    container = [[UIView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, )];
    [self.view addSubview:container];
}
- (void)progressView{
    progress = [[UIProgressView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width - , )];
    progress.progress = ;
    progress.progressTintColor = [UIColor orangeColor];
    progress.trackTintColor = [UIColor grayColor];
    [self.view addSubview:progress];

    playOrPause = [UIButton buttonWithType:UIButtonTypeCustom];
    playOrPause.frame = CGRectMake(, , self.view.frame.size.width - , );
    playOrPause.selected = NO;
    playOrPause.backgroundColor = [UIColor orangeColor];
    [playOrPause setTitle:@"播放" forState:UIControlStateNormal];
    [playOrPause setTitle:@"暂停" forState:UIControlStateSelected];
    [playOrPause addTarget:self action:@selector(playControlClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playOrPause];
}
- (void)playControlClicked:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [player play];
    }else{
        [player pause];
    }
}
- (void)setUpPlayer{
    NSURL *url = [NSURL URLWithString:@"http://7s1sjv.com2.z0.glb.qiniucdn.com/9F76A25D-9509-DEE9-3D8A-E93F360BB0E5.mp4"];
    playerItem = [AVPlayerItem playerItemWithURL:url];
    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];// 监听status属性
    [playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];// 监听缓冲进度
    //给AVPlayerItem添加播放完成通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

    player = [AVPlayer playerWithPlayerItem:playerItem];

    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.frame = container.frame;
    [container.layer addSublayer:playerLayer];
}
-(void)playbackFinished:(NSNotification *)notification{
    NSLog(@"视频播放完成.");
}
#pragma mark - KVO - status
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    AVPlayerItem *item = (AVPlayerItem *)object;
    if ([keyPath isEqualToString:@"status"]) {
            if ([item status] == AVPlayerStatusReadyToPlay) {
                NSLog(@"AVPlayerStatusReadyToPlay");
                CMTime duration = item.duration;// 获取视频总长度
                NSLog(@"%f", CMTimeGetSeconds(duration));
            }else if([item status] == AVPlayerStatusFailed) {
                NSLog(@"AVPlayerStatusFailed");
            }else{
                NSLog(@"AVPlayerStatusUnknown");
            }
    }else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
    }
}
@end

最新文章

  1. JavaScript 事件管理
  2. Spark on Yarn:java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost\\db_instance_name:1433;databaseName=db_name
  3. MyEclipse 下 Tomcat启动变慢如何解决
  4. C++中巧妙的位运算
  5. DEVMODE 结构体
  6. IL2CPP的前世今生
  7. [代码]Java后台推送消息到IOS前端
  8. [cocos2dx笔记008]cocos2d 用luabridge手动绑定类
  9. Android 四大组件学习之ContentProvider四
  10. IE兼容事件绑定V1.0
  11. python调试pdb
  12. MyEclipse和Eclipse
  13. Python10/23--继承/派生
  14. CSS-background-position百分比
  15. Linux内核系统体系概述
  16. [py]python的继承体系-源码目录结构
  17. ubuntu下安装go语言;sublime+gocode搭建;go的卸载和环境变量配个人.bashrc;2空位3个网
  18. ZJOI2018外省选手酱油记Day1
  19. fatal: incorrect checksum in control file
  20. POJ3907 Build Your Home

热门文章

  1. 基于python第三方requests 模块的HTTP请求类
  2. 数据结构之Heap (Java)
  3. 数据结构之Binary Search Tree (Java)
  4. less基本语法
  5. 【锋利的Jquery】读书笔记六
  6. HDU 1045 Fire Net(DFS)
  7. 使用rsync无密码传输
  8. [SOJ]统计数字
  9. 关于sbutils中的sblaunch插件的疑惑
  10. 【git】TurtoiseGit使用手册