AVFoundation--AVPlayer
//
// 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
AVFoundation--AVPlayer的更多相关文章
- AVPlayer
AVPlayer AVPlayerLayer是CALayer的一个子类,由于AVPlayer这个播放器只能安置在AVPlayerLayer 这个图层之上,所以我们需要实例化一个UIView,并 ...
- 基于 AVPlayer 自定义播放器
如果我只是简单的播放一个视频,而不需要考虑播放器的界面.iOS9.0 之前使用 MPMoviePlayerController, 或者内部自带一个 view 的 MPMoviePlayerViewCo ...
- iOS:基于AVPlayer实现的视频播放器
最近在学习AVFoundation框架的相关知识,写了一个基于AVPlayer的视频播放器,相关功能如下图: 代码github:https://github.com/wzpziyi1/VideoPla ...
- iOS开发 - AVPlayer实现流音频边播边存
边播边下有三套左右实现思路,本文使用AVPlayer + AVURLAsset实现. 概述 1. AVPlayer简介 AVPlayer存在于AVFoundation中,可以播放视频和音频,可以理解为 ...
- AVPlayer的使用本地视频
1引入AVFoundation.framework框架 2引入头文件<AVFoundation/AVFoundation.h>,并拖入需要播放的视频文件 代码如下: 自定义播放的View, ...
- iOS - AVPlayer 音视频播放
前言 NS_CLASS_AVAILABLE(10_7, 4_0) @interface AVPlayer : NSObject @available(iOS 4.0, *) public class ...
- iOS在线音乐播放SZKAVPlayer(基于AVPlayer的封装)
由于最近闲着没事,想找有关在线音乐播放的demo学习一下,在gitHub跟code4APP上面查找了很多帖子,结果很多在线音乐都是基于AudioStream实现的,我感觉用起来不太方便.后来突然发现, ...
- iOS的影片播放 MediaPlayer 和 AVPlayer(转)
分类: Iphone2013-01-28 16:19 5230人阅读 评论(0) 收藏 举报 在iOS開發上,如果遇到需要播放影片,如開機動畫…,我們很習慣地會使用MediaPlayer來播放影片,因 ...
- iOS 视频开发-AVPlayer
如果我只是简单的播放一个视频,而不需要考虑播放器的界面.iOS9.0 之前使用 MPMoviePlayerController, 或者内部自带一个 view 的 MPMoviePlayerViewCo ...
- 第六篇、AVplayer定制视频播放控件
1.引用头文件#import AVFoundation 2.自定义AVPlayer(播放的机器) 3.自定义AVPlayerItem(胶片) >> 视频的URL转成AVAsset 4.AV ...
随机推荐
- svn问题(队列)
svn执行clean up命令时报错"Previous operation has not finished; run 'cleanup' if it was interrupted&quo ...
- Maven之(二)Maven生命周期
我们在开发项目的时候,不断地在编译.测试.打包.部署等过程,maven的生命周期就是对所有构建过程抽象与统一,生命周期包含项目的清理.初始化.编译.测试.打包.集成测试.验证.部署.站点生成等几乎所有 ...
- 微软sqlHelper
//微软的SQLHelper类(含完整中文注释) using System; using System.Data; using System.Xml; using System.Data.SqlCli ...
- 最简单的jdbc操作
Connection con = null; Statement stmt = null; // 保存到表中 String sql = "insert into website(userna ...
- JS中的call()和apply()方法理解和使用
1.方法定义call方法: 语法:obj.method.call(thisObj[,arg1[, arg2[, [,.argN]]]]) 定义:调用对象(obj)的一个方法(method),以另一个对 ...
- strutsprepareAndExecuteFiter
FilterDispatcher是早期struts2的过滤器,后期的都用StrutsPrepareAndExecuteFilter了,如 2.1.6.2.1.8.StrutsPrepareAndExe ...
- 【 VS 插件开发 】三、Vs插件简单功能的实现
[ VS 插件开发 ]三.Vs插件简单功能的实现
- centos7如何安装pandoc
1 Install stack 1.1 Add the appropriate source repository: curl -sSL https://s3.amazonaws.com/downlo ...
- 面试题-Java基础-开发环境
1.什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行的字节码文件.Java被设 ...
- CSS IE的bug
没事总结一下IE的各种bug一下内容有一部分各位大神那里摘抄的,请恕小女子无罪: 1.ie6IE 6对margin的 auto 并没有正确的设置 解决方案:最简单的方法是在父元素中使用 text-al ...