日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AVPlayer播放视频

發布時間:2025/5/22 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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播放视频的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。