PlayVideoController.m
9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// PlayVideoController.m
// CNLiveRDVESDKDemo
//
// Created by iOS on 2017/11/28.
// Copyright © 2017年 zhulin. All rights reserved.
//
#import "PlayVideoController.h"
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <MediaPlayer/MediaPlayer.h>
#define PHOTO_ALBUM_NAME @"RDVEUISDKDemo"
@interface PlayVideoController ()
{
UIView *_playerView;
UIView *_syncContainer;
UIButton *_playBtn;
UIButton *_savePhotosAlbumBtn;
UIButton *_deletedBtn;
AVPlayer *_player;
id _timeObserver;
AVPlayerItem *playerItem;
UISlider *_slider;
float videoDuration;
NSTimer *playerTimer;
}
@end
@implementation PlayVideoController
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnterHome:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterForegroundNotification:) name:UIApplicationDidBecomeActiveNotification object:nil];
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
attributes[NSForegroundColorAttributeName] = UIColorFromRGB(0xffffff);
self.navigationController.navigationBar.titleTextAttributes = attributes;
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
NSLog(@"%s",__func__);
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
if (_timeObserver) {
[_player removeTimeObserver:_timeObserver];
_timeObserver = nil;
}
[_player pause];
_player = nil;
}
- (void)applicationEnterHome:(NSNotification *)notification{
[_player pause];
}
- (void)appEnterForegroundNotification:(NSNotification *)notification{
[_player play];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColorFromRGB(0x33333b);
self.title = @"播放";
[self initPlayerView];
[self initPlayer];
videoDuration = CMTimeGetSeconds(playerItem.duration);
}
- (void)initPlayer{
NSURL * url=[NSURL fileURLWithPath:_videoPath];
//创建item
playerItem=[[AVPlayerItem alloc]initWithURL:url];
//创建player
_player=[[AVPlayer alloc]initWithPlayerItem:playerItem];
//生成layer层
AVPlayerLayer * layer=[AVPlayerLayer playerLayerWithPlayer:_player];
//设置坐标
layer.frame=_playerView.bounds;
//把layer层加入到self.View中
[_playerView.layer addSublayer:layer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerToEnd) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
//进行播放
[_player play];
__block typeof(self) myself = self;
_timeObserver = [_player addPeriodicTimeObserverForInterval:CMTimeMake(1, 30) queue:NULL usingBlock:^(CMTime time) {
//当前播放时间
CGFloat currentime=CMTimeGetSeconds(time);
// NSLog(@"当前时间:%f",currentime);
//总时长
CMTime time1 = myself->playerItem.duration;
videoDuration=CMTimeGetSeconds(time1);
// NSLog(@"总时长:%f",myself->videoDuration);
//设置滑动条的进度
float v=currentime/videoDuration;
myself->_slider.value = v;
}];
videoDuration = CMTimeGetSeconds(playerItem.duration);
}
- (void) playWith{
[_player play];
}
- (void) initPlayerView{
_playerView= [[UIView alloc]init];
_playerView.frame=CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, ([UIScreen mainScreen].bounds.size.width));
_playerView.backgroundColor = [UIColor blackColor];
[self.view addSubview:_playerView];
_playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_playBtn.layer.cornerRadius = 5;
_playBtn.layer.masksToBounds = YES;
_playBtn.backgroundColor = UIColorFromRGB(0x0e0e10);
[_playBtn setTitle:@"播放" forState:UIControlStateNormal];
[_playBtn setTitle:@"暂停" forState:UIControlStateSelected];
[_playBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_playBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[_playBtn addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_playBtn];
_savePhotosAlbumBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_savePhotosAlbumBtn.backgroundColor = UIColorFromRGB(0x0e0e10);
[_savePhotosAlbumBtn setTitle:@"保存到相册" forState:UIControlStateNormal];
_savePhotosAlbumBtn.titleLabel.adjustsFontSizeToFitWidth =YES;
[_savePhotosAlbumBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_savePhotosAlbumBtn addTarget:self action:@selector(savePhotosAlbum) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_savePhotosAlbumBtn];
_savePhotosAlbumBtn.layer.cornerRadius = 5;
_savePhotosAlbumBtn.layer.masksToBounds = YES;
_playBtn.selected = YES;
_playBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-30, _playerView.frame.size.height+10, 60,40);
_savePhotosAlbumBtn.frame = CGRectMake(10, _playerView.frame.size.height+10, 80,40);
_slider = [[UISlider alloc] initWithFrame:CGRectMake(0, _playerView.frame.origin.y + _playerView.frame.size.height + 60, _playerView.frame.size.width, 25)];
_slider.maximumValue = 1;
_slider.minimumValue = 0;
_slider.backgroundColor = [UIColor clearColor];
[_slider addTarget:self action:@selector(seektime:) forControlEvents:UIControlEventValueChanged];
[self.view insertSubview:_slider aboveSubview:_playerView];
}
- (void) seektime: (UISlider *) sender{
float value = sender.value;
[playerItem seekToTime:CMTimeMakeWithSeconds(videoDuration*value, 600)];
}
- (void)playerToEnd{
[_player seekToTime:kCMTimeZero];
_slider.value = 0;
_playBtn.selected = NO;
}
- (void)freshSlider:(float)value{
_slider.value = value;
}
- (void)playVideo:(UIButton *)sender{
sender.selected = !sender.selected;
if(sender.selected){
[_player play];
}else{
[_player pause];
}
}
- (void)savePhotosAlbum{
[self saveVideoToCameraRoll:_videoPath];
}
-(void) saveVideoToCameraRoll:(NSString *)path{
if(_savePhotosAlbumBtn.selected){
return;
}
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__weak PlayVideoController *weakSelf = self;
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error){
if(error) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"保存提示" message:[NSString stringWithFormat:@"保存到相册失败!"] delegate:weakSelf cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
});
}
else {
__block ALAssetsGroup* groupToAddTo;
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:PHOTO_ALBUM_NAME]) {
groupToAddTo = group;
}
}
failureBlock:^(NSError* error) {
}];
[library assetForURL:assetURL resultBlock:^(ALAsset *addAsset){
BOOL suc=[groupToAddTo addAsset:addAsset];
if (suc) {
dispatch_async(dispatch_get_main_queue(), ^{
_savePhotosAlbumBtn.selected = YES;
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"保存提示" message:[NSString stringWithFormat:@"保存到相册成功!"] delegate:weakSelf cancelButtonTitle:@"我知道了" otherButtonTitles:nil];
[alert show];
});
}
}failureBlock:^(NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"保存提示" message:[NSString stringWithFormat:@"保存到相册失败!"] delegate:weakSelf cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
});
}];
}
}];
}
- (void)dealloc {
NSLog(@"%s",__func__);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end