-
Notifications
You must be signed in to change notification settings - Fork 6
/
CameraMic.hx
170 lines (148 loc) · 5.28 KB
/
CameraMic.hx
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
package;
#if mobile
import cpp.Lib;
#elseif neko
import neko.Lib;
#end
#if (android && openfl)
import lime.system.JNI;
#end
#if firefoxos
typedef MozActivity = {
var onsuccess:Void->Void;
var onerror:Void->Void;
}
#end
class CameraMic
{
@:isVar public static var appFilesDirectory(get, set):String = "";
private static var cameramic_setappfilesdirectory : Dynamic;
private static var cameramic_takephoto : Dynamic;
private static var cameramic_startrecordingaudio : Dynamic;
private static var cameramic_stoprecordingaudio : Dynamic;
private static var cameramic_playaudio : Dynamic;
private static var cameramic_stopaudio : Dynamic;
/**
* Function to push the native camera
* @param haxeObject Java will call the `cameraPhotoCallback` function in this object with the taken photo path
* @param callbackHandler Objective-C will call this function with the taken photo path
*/
public static function takePhoto(haxeObject:Dynamic, callbackHandler:Dynamic->?Bool->Void):Void
{
initCamera();
#if android
cameramic_takephoto(haxeObject);
#elseif ios
cameramic_takephoto(callbackHandler);
#elseif firefoxos
trace("takePhoto html5");
var emaitza:MozActivity = untyped __js__("new MozActivity( { name: 'pick', data: { type: ['image/png', 'image/jpg', 'image/jpeg'] }});");
emaitza.onsuccess = function() { callbackHandler(untyped __js__("this.result.blob")); };
emaitza.onerror = function() { untyped __js__("alert('Pick onerror funtzioan nago!');"); };
#end
}
/**
* Function to push the native microphone
* @param haxeObject Java will call the `recordAudioCallback` function in this object with the recorded audio path
* @param callbackHandler Objective-C will call this function with the recorded audio path
*/
public static function startRecordingAudio(haxeObject:Dynamic, callbackHandler:Dynamic->?Bool->Void, removeLastRecording:Bool = false):Void
{
initMic();
#if android
cameramic_startrecordingaudio(haxeObject, removeLastRecording);
#elseif ios
cameramic_startrecordingaudio(callbackHandler);
#end
}
/**
* Stop recording native microphone
*/
public static function stopRecordingAudio():Void
{
initMic();
#if mobile
cameramic_stoprecordingaudio();
#end
}
/**
* Play audio in native player
* @param filePath The path of the audio file
*/
public static function playAudio(filePath:String):Void
{
initPlayer();
#if mobile
cameramic_playaudio(filePath);
#end
}
public static function stopAudio():Void
{
initPlayer();
#if mobile
cameramic_stopaudio();
#end
}
private static function initAppDirectoryPath()
{
if (cameramic_setappfilesdirectory != null)
return;
#if android
cameramic_setappfilesdirectory = JNI.createStaticMethod("org.haxe.extension.cameramic.CameraMic", "setAppDirectory", "(Ljava/lang/String;)Ljava/lang/String;");
#elseif ios
cameramic_setappfilesdirectory = Lib.load ("cameramic", "cameramic_setappfilesdirectory", 1);
#end
}
private static function initCamera()
{
if (cameramic_takephoto != null)
return;
#if android
cameramic_takephoto = JNI.createStaticMethod("org.haxe.extension.cameramic.CameraMic", "takePhoto", "(Lorg/haxe/lime/HaxeObject;)V");
#elseif ios
cameramic_takephoto = Lib.load ("cameramic", "cameramic_takephoto", 1);
#end
}
private static function initMic()
{
if (cameramic_startrecordingaudio != null && cameramic_stoprecordingaudio != null)
return;
#if android
cameramic_startrecordingaudio = JNI.createStaticMethod("org.haxe.extension.cameramic.CameraMic", "startRecordingAudio", "(Lorg/haxe/lime/HaxeObject;Z)V");
cameramic_stoprecordingaudio = JNI.createStaticMethod("org.haxe.extension.cameramic.CameraMic", "stopRecordingAudio", "()V");
#elseif ios
cameramic_startrecordingaudio = Lib.load ("cameramic", "cameramic_startrecordingaudio", 2);
cameramic_stoprecordingaudio = Lib.load ("cameramic", "cameramic_stoprecordingaudio", 0);
#end
}
private static function initPlayer()
{
if (cameramic_playaudio != null && cameramic_stopaudio != null)
return;
#if android
cameramic_playaudio = JNI.createStaticMethod("org.haxe.extension.cameramic.CameraMic", "playAudio", "(Ljava/lang/String;)V");
cameramic_stopaudio = JNI.createStaticMethod("org.haxe.extension.cameramic.CameraMic", "stopAudio", "()V");
#elseif ios
cameramic_playaudio = Lib.load ("cameramic", "cameramic_playaudio", 1);
cameramic_stopaudio = Lib.load ("cameramic", "cameramic_stopaudio", 0);
#end
}
/**
* GETTERS & SETTERS
*/
public static function get_appFilesDirectory():String
{
if (appFilesDirectory != "") return appFilesDirectory;
return appFilesDirectory = "";
}
public static function set_appFilesDirectory(subdir:String):String
{
#if mobile
initAppDirectoryPath();
appFilesDirectory = cameramic_setappfilesdirectory(subdir);
#elseif flash
appFilesDirectory = subdir;
#end
return appFilesDirectory;
}
}