forked from wjhwsh/VideoPlayer-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoviePlayerController.m
331 lines (280 loc) · 7.99 KB
/
MoviePlayerController.m
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//
// MediaPlayController.m
// FFmpegPlayTest
//
// Created by Jack on 11/2/12.
// Copyright (c) 2012 Jack. All rights reserved.
//
#import "MoviePlayerController.h"
#import "VideoScreen.h"
#import "Decoder.h"
#import "AQHandler.h"
#pragma mark - Private Extension
@interface MoviePlayerController ()
<VideoScreenDelegate>
{
BOOL screenOn;
BOOL speakerOn;
}
@property (nonatomic, strong) VideoScreen* videoScreen;
@property (nonatomic, strong) AQHandler* audioScreen;
@property (nonatomic, strong) Decoder* decoder;
@end
#pragma mark - General Interface implementation
@implementation MoviePlayerController
@synthesize audioScreen = _audioScreen;
@synthesize videoScreen = _videoScreen;
@synthesize decoder = _decoder;
@synthesize contentURL = _contentURL;
@synthesize backgroundView = _backgroundView;
@synthesize loadState = _loadState;
@synthesize controlStyle = _controlStyle;
@synthesize repeatMode = _repeatMode;
@synthesize
scalingMode=_scalingMode,
readyForDisplay=_readyForDisplay;
@synthesize view;
- (UIView*) view
{
return _videoScreen;
}
//------------------------------------------------------------
@synthesize shouldAutoplay = _shouldAutoplay;
//- (void) setShouldAutoplay:(BOOL)shouldAutoplay
//{
// _shouldAutoplay = shouldAutoplay;
// if (_shouldAutoplay && [self isPreparedToPlay] ) {
// [self play];
// }
//}
//------------------------------------------------------------
- (BOOL) isFullscreen
{
//TODO: Need implementation
return YES;
};
//------------------------------------------------------------
- (void) setFullscreen:(BOOL)fullscreen animated:(BOOL)animated
{
//TODO: Need implementation
}
#pragma mark Initialize and utilities
//------------------------------------------------------------
- (id) initWithContentURL:(NSURL *)url
{
self = [super init];
if (self) {
_contentURL = url;
_playbackState = MPMoviePlaybackStateStopped;
_loadState = MPMovieLoadStateUnknown;
_controlStyle = MPMovieControlStyleDefault;
_repeatMode = MPMovieRepeatModeNone;
_shouldAutoplay = YES;
_fullscreen = NO;
_scalingMode = MPMovieScalingModeAspectFit;
_readyForDisplay = NO;
[self initDecoder];
if (!_decoder) return nil;
[self initAQHandler];
if (!_audioScreen)
{
_decoder = nil;
return nil;
}
[self initVideoScreen];
if (!_videoScreen)
{
_decoder = nil;
_audioScreen = nil;
return nil;
}
screenOn = YES;
speakerOn = YES;
_loadState = MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK;
NSLog(@"Finished init playController");
}
return self;
}
//------------------------------------------------------------
- (void) initDecoder
{
_decoder = [[Decoder alloc] initWithContentURL:_contentURL];
}
//------------------------------------------------------------
- (void) initAQHandler
{
_audioScreen = [[AQHandler alloc] initWithSource:_decoder];
}
//------------------------------------------------------------
- (void) initVideoScreen
{
_videoScreen = [[VideoScreen alloc] initWithSource: _decoder
delegate: self
scalingMode: _scalingMode];
//[_videoScreen setFrame:CGRectMake(0, 0, 320, 250)];
// TODO: init background view
}
//----------------------------------------------------------
#pragma mark VideoScreen Delegate
- (void) screenWillShow: (VideoScreen*) screen
{
}
//----------------------------------------------------------
- (void) screenDidShow:(VideoScreen*) screen
{
// Play video if autoplay is enabled
if (_shouldAutoplay) {
if ([self isPreparedToPlay] &&
(( _loadState & (MPMovieLoadStatePlayable | MPMovieLoadStatePlaythroughOK))!=0)){
[self play];
}
}
}
#pragma mark MPMediaPlayback protocol implementation
@synthesize isPreparedToPlay;
@synthesize currentPlaybackRate;
@synthesize currentPlaybackTime;
//------------------------------------------------------------
- (void) prepareToPlay
{
if (![self isPreparedToPlay]) {
// TODO: Need implementation
// Prepare to play
// Interrupt any active non-mixible audio session
// 1. prepare audioScreen
// 2. Prepare videoScreen
}
}
//------------------------------------------------------------
- (BOOL) isPreparedToPlay
{
return ([_decoder isReady] && [_audioScreen isReady] && [_videoScreen isReady]);
// return FALSE;
}
//------------------------------------------------------------
- (void) play
{
if ((_playbackState == MPMoviePlaybackStatePaused) ||
(_playbackState == MPMoviePlaybackStateStopped))
{
if (![self isPreparedToPlay]) {
[self prepareToPlay];
// TODO: Check result of preparation
}
[_decoder start];
if (screenOn) {
[_videoScreen start];
}
if (speakerOn) {
[_audioScreen start];
}
_playbackState = MPMoviePlaybackStatePlaying;
}
}
//------------------------------------------------------------
- (void) pause
{
[_decoder pause];
}
//------------------------------------------------------------
- (void) stop
{
// TODO: check to stop the running instance only
[_decoder stop];
[_videoScreen stop];
[_audioScreen stop];
_playbackState = MPMoviePlaybackStateStopped;
}
//------------------------------------------------------------
- (NSTimeInterval) currentPlaybackTime
{
//TODO: Need implementation
return [_decoder currentPlaybackTime];
}
//------------------------------------------------------------
- (float) currentPlaybackRate
{
return [_decoder currentPlaybackRate];
}
//------------------------------------------------------------
- (void) beginSeekingForward
{
//TODO: Need implementation
}
//------------------------------------------------------------
- (void) beginSeekingBackward
{
//TODO: Need implementation
}
//------------------------------------------------------------
- (void) endSeeking
{
//TODO: Need implementation
}
@end
//TODO: Need implementation
//MP_EXTERN NSString *const MPMediaPlaybackIsPreparedToPlayDidChangeNotification NS_AVAILABLE_IOS(3_2);
#pragma mark - Movie Properties Category implementaiton
@implementation MoviePlayerController (MovieProperties)
@dynamic
movieMediaTypes,
movieSourceType,
duration,
playableDuration,
naturalSize,
initialPlaybackTime,
endPlaybackTime;
- (MPMovieMediaTypeMask) movieMediaTypes
{
return [_decoder mediaTypes];
}
- (MPMovieSourceType) movieSourceType
{
//TODO checking source type base on URL
return [_decoder mediaSourceType];
}
- (void) setMovieSourceType:(MPMovieSourceType)movieSourceType
{
return [_decoder setMediaSourceType:movieSourceType];
}
- (NSTimeInterval) duration
{
return [_decoder duration];
}
- (NSTimeInterval) playableDuration
{
return [_decoder playableDuration];
}
- (CGSize) naturalSize
{
return [_decoder videoSize];
}
- (NSTimeInterval) initialPlaybackTime
{
return [_decoder startTime];
}
- (NSTimeInterval) endPlaybackTime
{
return [_decoder endTime];
}
#pragma mark - Thumbnail Generation Category
// Returns a thumbnail at the given time.
- (UIImage *)thumbnailImageAtTime:(NSTimeInterval)playbackTime
timeOption:(MPMovieTimeOption)option
{
// TODO: need implementation
return nil;
}
// Asynchronously request thumbnails for one or more times, provided as an array of NSNumbers (double).
// Posts MPMoviePlayerThumbnailImageRequestDidFinishNotification on completion.
- (void)requestThumbnailImagesAtTimes:(NSArray *)playbackTimes
timeOption:(MPMovieTimeOption)option
{
// TODO: Need implementation
}
// Cancels all pending asynchronous thumbnail requests.
- (void)cancelAllThumbnailImageRequests
{
//TODO: Need implementation
}
@end