iOS-AVPlayer使用
1引入AVFoundation.framework框架
2引入头文件<AVFoundation/AVFoundation.h>,并拖入需要播放的视频文件
代码如下:
自定义播放的View,PlayerView
PlayerView.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface PlayerView : UIView
//播放器和当前view进行关联
- (void)setPlayer:(AVPlayer *)player;
@end
PlayerView.m文件代码如下:
#import "PlayerView.h"
@implementation PlayerView
+ (Class)layerClass {
return [AVPlayerLayer class];
}
- (void)setPlayer:(AVPlayer *)player {
[(AVPlayerLayer *)[self layer] setPlayer:player];
}
@end
控制器代码如下:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "PlayerView.h"
@interface ViewController ()
{
PlayerView* _playerView;
UISlider* _proSlider;
AVPlayer* _player;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_playerView = [[PlayerView alloc] initWithFrame:CGRectMake(0, 20, 320, 240)];
[self.view addSubview:_playerView];
_proSlider = [[UISlider alloc] initWithFrame:CGRectMake(50, 300, 220, 20)];
_proSlider.minimumValue = 0.0;
_proSlider.maximumValue = 1.0;
_proSlider.value = 0.0;
[_proSlider addTarget:self action:@selector(setProgress) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_proSlider];
UIButton* playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
playButton.frame = CGRectMake(110, 340, 100, 40);
[playButton setTitle:@"play" forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
UIButton* stopButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
stopButton.frame = CGRectMake(110, 390, 100, 40);
[stopButton setTitle:@"stop" forState:UIControlStateNormal];
[stopButton addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:stopButton];
NSString* path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp4"];
NSURL* url = [NSURL fileURLWithPath:path];
//资源类
AVURLAsset* asset = [AVURLAsset assetWithURL:url];
//加载
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{
//得到状态
AVKeyValueStatus status = [asset statusOfValueForKey:@"tracks" error:nil];
if (status == AVKeyValueStatusLoaded) {
//AVPlayer->AVPlayerItem->AVURLAsset
AVPlayerItem* item = [AVPlayerItem playerItemWithAsset:asset];
//播放器
_player = [[AVPlayer alloc] initWithPlayerItem:item];
//播放器进行关联
[_playerView setPlayer:_player];
//进度监听
//CMTime 1帧 1帧/每秒
[_player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_global_queue(0, 0) usingBlock:^(CMTime time) {
//当前时间 / 总时间
CMTime currentTime = _player.currentItem.currentTime;
CMTime duration = _player.currentItem.duration;
float pro = CMTimeGetSeconds(currentTime) / CMTimeGetSeconds(duration);
dispatch_async(dispatch_get_main_queue(), ^{
[_proSlider setValue:pro animated:YES];
});
}];
}
}];
}
//设置进度
- (void)setProgress{
//总时间 * 进度 = 当前时间
CMTime duration = _player.currentItem.duration;
CMTime currentTime = CMTimeMultiplyByFloat64(duration, _proSlider.value);
//从当前时间播放
[_player seekToTime:currentTime];
}
- (void)play{
[_player play];
}
- (void)stop{
[_player pause];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS-AVPlayer使用的更多相关文章
- iOS AVPlayer视频播放器
代码地址如下:http://www.demodashi.com/demo/11168.html 一.运行效果 二.实现过程 ①.创建播放器avPlayer //创建播放器 url = [url str ...
- iOS - AVPlayer 音视频播放
前言 NS_CLASS_AVAILABLE(10_7, 4_0) @interface AVPlayer : NSObject @available(iOS 4.0, *) public class ...
- iOS AvPlayer AvAudioPlayer音频的后台播放问题
iOS 4开始引入的multitask,我们可以实现像ipod程序那样在后台播放音频了.如果音频操作是用苹果官方的AVFoundation.framework实现,像用AvAudioPlayer,Av ...
- 【转】iOS:AvPlayer设置播放速度不生效的解决办法
现象: 项目有一个需求是实现视频的慢速播放,使用的是封装的AvPlayer,但是设置时发现比如设置rate为0.5,0.1,0.01都是一样的速度,非常疑惑.后来经过查找资料,发现iOS10对这个AP ...
- iOS AVPlayer 学习
1 .使用环境: 在实际开发过程中 有需要展示流媒体的模块 ,需求非常简单 :播放 和 暂停 ,其实这个时候有很多选择 ,可以选择 MPMoviePlayerController(MediaPlaye ...
- iOS AVPlayer 的使用实践
前两天在网上看到一篇博客,介绍AVPlayer的使用,但是只简单介绍了一下单个的本地文件如何播放,心血来潮,就想着做一个类似于播放器的东西,能够实现播放网络歌曲,循环播放多首音乐,下面我们来实现一下 ...
- ios AVPlayer参考
http://www.cnblogs.com/kenshincui/p/4186022.html#avPlayer
- ios avplayer 监控播放进度
var timeObserver = avPlayerVC.player?.addPeriodicTimeObserver(forInterval: CMTime.init(value: , ti ...
- iOS AVKit音视频播放全面详解
公司项目中经常要用到音视频处理,也需要去定制一些东西,然后整理这些音视频处理就显得尤为重要!方便自己和广大朋友学习收藏! 以下参考连接特别重要: 苹果官方:AVKit API 苹果官方:AVFound ...
- iOS 视频全屏功能 学习
项目中,也写过类似"视频全屏"的功能, 前一阵子读到今日头条 的一篇技术文章,详细介绍三种旋转方法差异优劣最终择取.文章从技术角度看写的非常好,从用户角度看,也用过多家有视频功能的 ...
随机推荐
- STM32的指令周期
在keil中编程时,写了一行代码,然后就想知道,执行这句C代码需要多长时间. 时钟周期在这就不解释了,频率的倒数. 指令周期,个人理解就是cpu执行一条汇编指令所需要的时间. 我们知道cm3使用的三级 ...
- TDOA 之 基站逻辑代码实现
在前一篇博文里描述了基站的逻辑部分,这里贴出来具体代码实现.https://www.cnblogs.com/tuzhuke/p/11689881.html 1 Sync 信息部分 case 'S': ...
- 使用JSP/Servalet技术开发新闻发布系统------动态网页开发基础
什么是动态网页? 动态网页是指在服务器端运行的程序或者网页,它们会随不同客户.不同时间,返回不同的网页. 动态网页的特点? (1).交互性:即网页会根据用户的要求和选择而动态改变和响应.采用动态网页技 ...
- 快速幂C++实现
快速幂模板题 很明显,这个题目不能用简单的\(for\)循环+\(mod\)来完成,因为指数\(p\)已经达到了长整型,直接循环来完成的话肯定会超时的. 那么快速幂就应运而生了. 什么是快速幂呢? 利 ...
- 监控ntp进程的
!#/bin/bash ntp_num=$[`ps -ef|grep ntp|wc -l`-1] if [ $ntp_num == 1 ];then echo 0 else echo $ntp_num ...
- 代码 | 自适应大邻域搜索系列之(6) - 判断接受准则SimulatedAnnealing的代码解析
前言 前面三篇文章对大家来说应该很简单吧?不过轻松了这么久,今天再来看点刺激的.关于判断接受准则的代码.其实,判断接受准则有很多种,效果也因代码而异.今天介绍的是模拟退火的判断接受准则.那么,相关的原 ...
- [转]C++ 类中的static成员的初始化和特点
在C++的类中有些成员变量初始化和一般数据类型的成员变量有所不同.以下测试编译环境为: ➜ g++ -v Using built-in specs. COLLECT_GCC=g++ Target: x ...
- linux下安装python3.6.6
1.到python的官网去下载python3.6.3安装包,必须是Linux版本的 2.在/usr/tmp下下载python安装包 wget https://www.python.org/ftp/py ...
- ROS手动编写消息发布器和订阅器topic demo(C++)
1.首先创建 package cd ~/catkin_ws/src catkin_create_pkg topic_demo roscpp rospy std_msgs 2. 编写 msg 文件 cd ...
- C++11多线程std::thread创建方式
//#include <cstdlib> //#include <cstdio> //#include <cstring> #include <string& ...