-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCVideoPlayerImpliOS.m
executable file
·165 lines (131 loc) · 4.8 KB
/
CCVideoPlayerImpliOS.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
/*
* CCVideoPlayer
*
* Cocos2D-iPhone-Extensions v0.2.1
* https://github.com/cocos2d/cocos2d-iphone-extensions
*
* Copyright (c) 2010-2012 Stepan Generalov
* Copyright (c) 2011 Patrick Wolowicz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
#import "cocos2d.h"
#import "CCVideoPlayer.h"
#import "CCVideoPlayerImpliOS.h"
#import "MediaPlayer/MediaPlayer.h"
#import "videoOverlayView.h"
@interface CCVideoPlayerImpliOS ()
@property (nonatomic, strong) MPMoviePlayerController* theMovie;
@property (nonatomic, strong) VideoOverlayView* videoOverlayView;
@end
@implementation CCVideoPlayerImpliOS
@synthesize isPlaying = _playing;
- (id) init
{
if ( (self = [super init]) )
{
self.theMovie = nil;
}
return self;
}
- (void) setupViewAndPlay
{
//UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* keyWindow = [CCDirector sharedDirector].view;
[keyWindow addSubview:self.theMovie.view];
self.theMovie.view.hidden = NO;
self.theMovie.view.frame = keyWindow.bounds;
self.theMovie.view.center = keyWindow.center;
self.theMovie.view.contentMode = UIViewContentModeScaleAspectFit;
// Movie playback is asynchronous, so this method returns immediately.
[self.theMovie play];
self.videoOverlayView = [ [VideoOverlayView alloc] initWithFrame:self.theMovie.view.frame];
[keyWindow addSubview:self.videoOverlayView];
}
//----- playMovieAtURL: ------
-(void)playMovieAtURL:(NSURL*)theURL
{
_playing = YES;
[self.delegate movieStartsPlaying]; // RAG: Seems premature - shouldn't this be in the setupViewAndPlay where it actually starts?
self.theMovie = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
if (! self.theMovie)
_playing = NO;
self.theMovie.controlStyle = MPMovieControlStyleNone; // TODO: Expose this to caller??
// Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.theMovie];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(preparedToPlayCallback:)
name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:self.theMovie];
[self.theMovie prepareToPlay];
}
//----- movieFinishedCallback -----
-(void)movieFinishedCallback:(NSNotification*)aNotification
{
MPMoviePlayerController* theMovie = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
// Release the movie instance created in playMovieAtURL:
[self.theMovie.view removeFromSuperview];
self.theMovie = nil;
_playing = NO;
[self.videoOverlayView removeFromSuperview];
self.videoOverlayView = nil;
[self.delegate moviePlaybackFinished];
}
-(void)preparedToPlayCallback:(NSNotification*)aNotification
{
MPMoviePlayerController* theMovie = [aNotification object];
if (theMovie.isPreparedToPlay)
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
object:theMovie];
[self setupViewAndPlay];
}
}
- (void) setNoSkip:(BOOL)value;
{
noSkip=value;
}
- (void) userCancelPlaying
{
if (!noSkip)
{
BOOL cancelPlaying = YES;
if ([self.delegate respondsToSelector:@selector(userTouch)])
{
cancelPlaying = [self.delegate userTouch];
}
if (cancelPlaying)
[self cancelPlaying];
}
}
- (void) cancelPlaying
{
[self.theMovie stop];
}
@end
#endif