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使用的更多相关文章

  1. iOS AVPlayer视频播放器

    代码地址如下:http://www.demodashi.com/demo/11168.html 一.运行效果 二.实现过程 ①.创建播放器avPlayer //创建播放器 url = [url str ...

  2. iOS - AVPlayer 音视频播放

    前言 NS_CLASS_AVAILABLE(10_7, 4_0) @interface AVPlayer : NSObject @available(iOS 4.0, *) public class ...

  3. iOS AvPlayer AvAudioPlayer音频的后台播放问题

    iOS 4开始引入的multitask,我们可以实现像ipod程序那样在后台播放音频了.如果音频操作是用苹果官方的AVFoundation.framework实现,像用AvAudioPlayer,Av ...

  4. 【转】iOS:AvPlayer设置播放速度不生效的解决办法

    现象: 项目有一个需求是实现视频的慢速播放,使用的是封装的AvPlayer,但是设置时发现比如设置rate为0.5,0.1,0.01都是一样的速度,非常疑惑.后来经过查找资料,发现iOS10对这个AP ...

  5. iOS AVPlayer 学习

    1 .使用环境: 在实际开发过程中 有需要展示流媒体的模块 ,需求非常简单 :播放 和 暂停 ,其实这个时候有很多选择 ,可以选择 MPMoviePlayerController(MediaPlaye ...

  6. iOS AVPlayer 的使用实践

    前两天在网上看到一篇博客,介绍AVPlayer的使用,但是只简单介绍了一下单个的本地文件如何播放,心血来潮,就想着做一个类似于播放器的东西,能够实现播放网络歌曲,循环播放多首音乐,下面我们来实现一下 ...

  7. ios AVPlayer参考

    http://www.cnblogs.com/kenshincui/p/4186022.html#avPlayer

  8. ios avplayer 监控播放进度

      var timeObserver = avPlayerVC.player?.addPeriodicTimeObserver(forInterval: CMTime.init(value: , ti ...

  9. iOS AVKit音视频播放全面详解

    公司项目中经常要用到音视频处理,也需要去定制一些东西,然后整理这些音视频处理就显得尤为重要!方便自己和广大朋友学习收藏! 以下参考连接特别重要: 苹果官方:AVKit API 苹果官方:AVFound ...

  10. iOS 视频全屏功能 学习

    项目中,也写过类似"视频全屏"的功能, 前一阵子读到今日头条 的一篇技术文章,详细介绍三种旋转方法差异优劣最终择取.文章从技术角度看写的非常好,从用户角度看,也用过多家有视频功能的 ...

随机推荐

  1. How To Use The Repository Pattern In Laravel

    The Repository Pattern in Laravel is a very useful pattern with a couple of great uses. The first us ...

  2. 搭建自己的博客(八):使用fontawesome框架来添加图标以及美化详情页

    在网页中有时候会使用到图标,自己弄图标有些麻烦所以就使用了fontawesome框架. 官网:   下载地址 我使用的fontawesome版本是5.5.0版本 1.先上变化的部分

  3. 带着历史提交记录迁移git仓库

    1. git push --mirror --mirror模式会把本地的分支都克隆 // 先用--bare克隆裸仓库 git clone git@gitee.com:zhangamie/testApp ...

  4. ubuntu ukylin wineqq 登录时提示:您的号码暂时不能使用低版本的qq

    ubuntu ukylin wineqq 登录时提示:您的号码暂时不能使用低版本的qq,而有的qq号登录没有问题. 优麒麟官网上下载的qqwine安装包,解压后安装三个deb包. 郁闷了一下午,都想装 ...

  5. 有没有一个工具可以帮助查找python的bug和进行静态的代码分析?

    答:PyChecker是一个python代码的静态分析工具,它可以帮助查找python代码的bug, 会对代码的复杂度和格式提出警告 Pylint是另外一个工具可以进行codingstandard检查

  6. C++中的平方、开方、绝对值怎么计算

    #include <math.h> //平方 pow() ,);// 4的平方=16 //开方 ,0.5);// 4的平方根=2 );// 4的平方根=2 //整数绝对值 int c = ...

  7. 对list某个条件排序,并实现分页

    package com.jcloud.aioc.api.controller.Test; import com.alibaba.fastjson.JSON; import org.apache.poi ...

  8. 搭建docusaurus博客

    搭建docusaurus博客: docusaurus是facebook的开源的用于简化构建,部署,和维护的博客网站. 特点: 快速启动 支持本地化 页面可自定义 安装要求: Node >= 8. ...

  9. linux 网络带宽和延时测试

    Linux下使用qperf命令来测试网络带宽和网络延迟 参考文章:https://access.redhat.com/solutions/2122681 若是没有安装qperf命令,请使用yum 安装 ...

  10. C语言--输入输出格式

    一.PTA实验作业 题目1:7-3 温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1 ...