-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpage_arcloud.dart
216 lines (180 loc) · 6.55 KB
/
page_arcloud.dart
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
import 'dart:async';
import 'package:banuba_arcloud/banuba_arcloud.dart';
import 'package:banuba_sdk/banuba_sdk.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'main.dart';
class ARCloudPage extends StatefulWidget {
const ARCloudPage({super.key});
@override
State<ARCloudPage> createState() => _ARCloudPageState();
}
class _ARCloudPageState extends State<ARCloudPage> with WidgetsBindingObserver {
static const _tag = 'ARCloud';
// Bucket with Demo effects, can be replaced by your own AR Cloud URL
static const _arCloudUrl = 'https://api.arcloud.banuba.net/v1/effects/far_test_v1.2';
bool _isProcessing = false;
final BanubaSdkManager _banubaSdkManager = BanubaSdkManager();
final _epWidget = EffectPlayerWidget(key: null);
final _arCloudPlugin = BanubaARCloudPlugin();
late StreamSubscription<void> _effectsStreamSubscription;
Effect? _effectToDownload;
bool requestToDownload = false;
@override
void initState() {
debugPrint('$_tag: init');
super.initState();
initSDK();
}
@override
void dispose() {
super.dispose();
debugPrint('$_tag: release SDK and AR Cloud');
// Dispose AR Cloud
_effectsStreamSubscription.cancel();
_arCloudPlugin.dispose();
// Dispose Banuba SDK
_banubaSdkManager.stopPlayer();
_banubaSdkManager.closeCamera();
_banubaSdkManager.deinitialize();
}
Future<void> initSDK() async {
debugPrint('$_tag: init SDK');
await _banubaSdkManager.initialize([], banubaToken, SeverityLevel.info);
// It is required to grant all permissions for the plugin: Camera
requestPermissions().then((granted) {
if (granted) {
debugPrint('$_tag: Thanks! Camera permission is granted!');
openCamera();
} else {
debugPrint('$_tag: WARNING! Camera permission is not granted!');
// Plugin cannot be used. Handle this state on your app side
SystemNavigator.pop();
}
}).onError((error, stackTrace) {
debugPrint('$_tag: ERROR! Plugin cannot be used : $error');
// Plugin cannot be used. Handle this state on your app side
SystemNavigator.pop();
});
// Subscribe to getting effect changes.
// List of effects contain local and remote effects.
// The list gets changes when effect is downloaded.
_effectsStreamSubscription = _arCloudPlugin.getEffectsStream().listen(
_handleLoadedEffects,
onError: (e) => _showMessage(e.toString()),
);
_arCloudPlugin.init(arCloudUrl: _arCloudUrl);
debugPrint('$_tag: SDK and ARCloud initialized successfully');
}
Future<void> openCamera() async {
debugPrint('$_tag: open camera');
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) {
debugPrint('$_tag: Warning! widget is not mounted!');
return;
}
await _banubaSdkManager.openCamera();
await _banubaSdkManager.attachWidget(_epWidget.banubaId);
await _banubaSdkManager.unloadEffect();
_banubaSdkManager.startPlayer();
}
Future<void> loadEffects() async {
debugPrint('$_tag: loadEffects');
_isProcessing = true;
// Reset load effect state
_effectToDownload = null;
requestToDownload = true;
setState(() {});
_arCloudPlugin.loadEffects();
}
Future<void> _handleLoadedEffects(List<Effect> allEffects) async {
debugPrint('$_tag: _handleLoadedEffects, effectToDownload = $_effectToDownload');
// The sample demonstrates how to download new effect from AR Cloud, how to apply once this
// effect is downloaded.
// Your implementation might be very different.
// Anyway you will need to operate with list of all effects - local, remote.
// The effect is considered as local once it is downloaded.
// You can apply or play only local effects.
if (allEffects.isEmpty) {
_isProcessing = false;
_showMessage("No available effects!");
setState(() {});
return;
}
// Check if there pending effect to download
if (_effectToDownload != null) {
final downloadedEffect =
allEffects.firstWhereOrNull((element) => element.eTag == _effectToDownload!.eTag);
debugPrint('$_tag: _handleLoadedEffects, downloadedEffect = $downloadedEffect');
if (downloadedEffect != null && downloadedEffect.isDownloaded) {
// Great! Effect is downloaded and prepared to be applied
_isProcessing = false;
_banubaSdkManager.loadEffect(downloadedEffect.uri, false);
setState(() {});
}
return;
}
if (requestToDownload) {
// Find the first not downloaded effect
_effectToDownload = allEffects.firstWhereOrNull((element) => !element.isDownloaded);
debugPrint('$_tag: Found effect to download = $_effectToDownload');
if (_effectToDownload == null) {
debugPrint('$_tag: No available effects to download');
return;
} else {
try {
// Download effect. The stream will be updated once the effect is downloaded
await _arCloudPlugin.downloadEffect(_effectToDownload!.name);
} on Exception catch (e) {
_showMessage(e.toString());
return;
} finally {
requestToDownload = false;
}
}
}
}
@override
Widget build(BuildContext context) {
final screenSize = MediaQuery.of(context).size;
debugPrint('$_tag: build: isProcessing = $_isProcessing');
return Stack(children: [
SizedBox(width: screenSize.width, height: screenSize.height, child: _epWidget),
Positioned(
bottom: screenSize.height * 0.03,
left: screenSize.width * 0.35,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
shape: const StadiumBorder(),
backgroundColor: Colors.green,
fixedSize: const Size(120, 40),
),
onPressed: () => loadEffects(),
child: Text(
"Load Effect".toUpperCase(),
style: const TextStyle(
fontSize: 10.0,
),
),
)),
Center(
child: Visibility(
visible: _isProcessing,
child: const CircularProgressIndicator(
color: Colors.green,
),
),
)
]);
}
void _showMessage(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
),
);
}
}