码迷,mamicode.com
首页 > 其他好文 > 详细

AVPlayer的使用本地视频

时间:2016-04-23 18:20:54      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

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

.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

AVPlayer的使用本地视频

标签:

原文地址:http://www.cnblogs.com/linxiu-0925/p/5425052.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!