AVPlayer播放视频
2019獨角獸企業重金招聘Python工程師標準>>>
//
//? ViewController.m
//? 04-AVPlayer播放視頻
//
//? Created by鹿微微鹿 on 16/5/6.
//? Copyright (c) 2016年 鹿微微鹿. All rights reserved.
//
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
{
? ? //播放器
? ? AVPlayer *_player;
? ? //顯示畫面的Layer
? ? AVPlayerLayer *imageLayer;
?? ?
? ? UIProgressView *progress;
}
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
?? ?
? ? NSString *path =
? ? ? ? @"http://otmv.alicdn.com/new/mv_1_6/23/77/2306cd13e3dde338b53f404ece43a277.mp4?k=7125c1a8f09b70bb&t=1451830866";
?? ?
? ? //1.創建item
? ? AVPlayerItem *item = [[AVPlayerItem alloc]
? ? ? ? ? ? ? ? ? ? ? ? ? initWithURL:[NSURL URLWithString:path]];
?? ?
? ? //2.創建播放器對象
? ? _player = [AVPlayer playerWithPlayerItem:item];
?? ?
?? ?
? ? //3.播放
? ? [_player play];
? ? //==================顯示圖像========================
? ? //4.創建顯示視頻的layer專門用來顯示視頻圖像的
? ? //參數:播放器
?? imageLayer ? = [AVPlayerLayer playerLayerWithPlayer:
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? _player];
?? ?
? ? //2.設置frame
? ? imageLayer.frame = self.view.bounds;
? ? //3.添加到界面上
? ? [self.view.layer addSublayer:imageLayer];
?? ?
? ? //4.獲取視頻界面的大小
? ? CGRect rect = imageLayer.videoRect;
?? ?
? ?
?? ?
?? ?
? ? //=========屏幕旋轉==================
? ? [[NSNotificationCenter defaultCenter]
?? ? addObserver:self selector:@selector(screenChangeSite) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
?? ?
}
- (void)viewWillAppear:(BOOL)animated{
? ? [super viewWillAppear:animated];
?? ? CGRect rect = imageLayer.videoRect;
? ? //5.創建一個進度條
? ? progress = [[UIProgressView alloc]
? ? ? ? ? ? ? ? initWithFrame:CGRectMake(rect.origin.x,rect.origin.y+rect.size.height, rect.size.width, 10)];
? ? [self.view addSubview:progress];
? ? ? __weak UIProgressView *p = progress;
? ? [_player addPeriodicTimeObserverForInterval:CMTimeMake(1,10 ) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
?? ? ?
? ? ? ? //更新進度
? ? ? ? //總時間
? ? ? float total =? _player.currentItem.duration.value / _player.currentItem.duration.timescale;
? ? ? ? //當前時間
? ? ? ? float current = time.value * 1.0f/time.timescale;
?? ? ? ?
? ? ? ? p.progress = current/total;
?? ? ? ?
? ? }];
?? ?
? ? //顯示樣式
? ? //AVLayerVideoGravityResizeAspect 默認的,沒有縮放
? ? //AVLayerVideoGravityResize:layer有多大就顯示多大視頻有縮放
? ? //AVLayerVideoGravityResizeAspectFill 按比例縮放,顯示不全
? ? [imageLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
?? ?
?? ?
}
#pragma mark - 屏幕旋轉frame改變
- (void)screenChangeSite{
?? ?
? ? NSLog(@"屏幕旋轉");
? ? imageLayer.frame = self.view.bounds;
?? ? CGRect rect = imageLayer.videoRect;
? ? progress.frame = CGRectMake(rect.origin.x,rect.origin.y+rect.size.height, rect.size.width, 10);
}
@end
轉載于:https://my.oschina.net/luhoney/blog/671226
總結
以上是生活随笔為你收集整理的AVPlayer播放视频的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初识tmux---编译安装tmux
- 下一篇: Objective-C笔记