forked from sintaxi/phonegap
/
Movie.m
executable file
·146 lines (113 loc) · 4.73 KB
/
Movie.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
/*
* Movie.m
*
* Created by Nitobi on 12/12/08.
* Copyright 2008 Nitobi. All rights reserved.
*
*/
#import "Movie.h"
#import <AudioToolbox/AudioServices.h>
#import <MediaPlayer/MediaPlayer.h>
@implementation Movie
@synthesize theMovie,stopReceived,repeat;
- (PhoneGapCommand*) initWithWebView:(UIWebView*)theWebView
{
self = (Movie*)[super initWithWebView:(UIWebView*)theWebView];
if (self) {
stopReceived = false;
repeat = false;
}
return self;
}
- (void) play:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSBundle * mainBundle = [NSBundle mainBundle];
NSMutableArray *directoryParts = [NSMutableArray arrayWithArray:[(NSString*)[arguments objectAtIndex:0] componentsSeparatedByString:@"/"]];
NSString *filename = [directoryParts lastObject];
[directoryParts removeLastObject];
NSMutableArray *filenameParts = [NSMutableArray arrayWithArray:[filename componentsSeparatedByString:@"."]];
NSString *directoryStr = [directoryParts componentsJoinedByString:@"/"];
NSString *filePath = [mainBundle pathForResource:(NSString*)[filenameParts objectAtIndex:0]
ofType:(NSString*)[filenameParts objectAtIndex:1]
inDirectory:directoryStr];
if (filePath == nil) {
filePath = [mainBundle pathForResource:(NSString*)[filenameParts objectAtIndex:0]
ofType:(NSString*)[filenameParts objectAtIndex:1]];
if (filePath == nil) {
NSLog(@"Can't find filename %@ in the app bundle", [arguments objectAtIndex:0]);
return;
}
}
// TODO Create a system facilitating handling callback responses in JavaScript easily, and no
// longer in an ad-hoc fashion. Getting error results of whether or not the Movie played, or
// other errors occurring in the system is important.
OSStatus error = AudioSessionInitialize(NULL, NULL, interruptionListener, self);
if (error) {
NSLog(@"ERROR INITIALIZING AUDIO SESSION! %d\n", error);
return;
}
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty ( kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory );
error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, propListener, self);
if (error) {
NSLog(@"ERROR Adding Property Listener %d\n", error);
return;
}
// AudioSessionSetActive (true);
theMovie = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath: filePath]];
NSLog(@"theMovie description = %@", [(NSObject *)theMovie description]);
//[theMovie setOrientation:UIDeviceOrientationPortrait animated:NO]; // TODO: remove? no such selector
theMovie.scalingMode = MPMovieScalingModeAspectFill;
theMovie.movieControlMode = MPMovieControlModeDefault;
// Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
// Movie playback is asynchronous, so this method returns immediately.
[theMovie play];
}
- (void) stop:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
NSLog(@"Stop the movie!");
stopReceived = true;
NSLog(@"theMovie description = %@", [(NSObject *)theMovie description]);
[theMovie stop];
NSLog(@"Finished stopping the movie.");
}
#pragma mark AudioSession listeners
void interruptionListener (
void *inClientData,
UInt32 inInterruptionState
)
{
if (inInterruptionState == kAudioSessionBeginInterruption) {}
NSLog(@"Movie: interruptionListener");
}
void propListener( void * inClientData,
AudioSessionPropertyID inID,
UInt32 inDataSize,
const void * inData)
{
NSLog(@"Movie: audio prop Listener");
}
- (void) myMovieFinishedCallback:(NSNotification*)aNotification
{
NSLog(@"myMovieFinishedCallback");
MPMoviePlayerController* myMovie = [aNotification object];
NSLog(@"myMovie description = %@, theMovie description = %@, stopReceived = %d", [(NSObject *)myMovie description], theMovie, stopReceived);
if(stopReceived) {
[theMovie stop];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:myMovie];
}
if(repeat) {
[myMovie play];
}
}
- (void) dealloc
{
[theMovie release];
[super dealloc];
}
@end