|
1 | 1 | #include "audioplayers_tizen_plugin.h"
|
2 | 2 |
|
3 |
| -#include <Ecore.h> |
4 | 3 | #include <flutter/encodable_value.h>
|
5 | 4 | #include <flutter/method_channel.h>
|
6 | 5 | #include <flutter/plugin_registrar.h>
|
|
15 | 14 |
|
16 | 15 | namespace {
|
17 | 16 |
|
18 |
| -const double kTimeInterval = 0.2; |
19 |
| - |
20 | 17 | template <typename T>
|
21 | 18 | static bool GetValueFromEncodableMap(const flutter::EncodableMap *map,
|
22 | 19 | const char *key, T &out) {
|
@@ -51,12 +48,7 @@ class AudioplayersTizenPlugin : public flutter::Plugin {
|
51 | 48 |
|
52 | 49 | AudioplayersTizenPlugin() {}
|
53 | 50 |
|
54 |
| - virtual ~AudioplayersTizenPlugin() { |
55 |
| - if (timer_) { |
56 |
| - ecore_timer_del(timer_); |
57 |
| - timer_ = nullptr; |
58 |
| - } |
59 |
| - } |
| 51 | + virtual ~AudioplayersTizenPlugin() {} |
60 | 52 |
|
61 | 53 | private:
|
62 | 54 | void HandleMethodCall(
|
@@ -208,9 +200,25 @@ class AudioplayersTizenPlugin : public flutter::Plugin {
|
208 | 200 | };
|
209 | 201 |
|
210 | 202 | StartPlayingListener start_playing_listener =
|
211 |
| - [plugin = this](const std::string &player_id) { |
212 |
| - ecore_main_loop_thread_safe_call_async(StartPositionUpdates, |
213 |
| - (void *)plugin); |
| 203 | + [channel = channel_.get()](const std::string &player_id, |
| 204 | + const int duration, const int position) { |
| 205 | + flutter::EncodableMap durationWrapped = { |
| 206 | + {flutter::EncodableValue("playerId"), |
| 207 | + flutter::EncodableValue(player_id)}, |
| 208 | + {flutter::EncodableValue("value"), |
| 209 | + flutter::EncodableValue(duration)}}; |
| 210 | + channel->InvokeMethod( |
| 211 | + "audio.onDuration", |
| 212 | + std::make_unique<flutter::EncodableValue>(durationWrapped)); |
| 213 | + |
| 214 | + flutter::EncodableMap positionWrapped = { |
| 215 | + {flutter::EncodableValue("playerId"), |
| 216 | + flutter::EncodableValue(player_id)}, |
| 217 | + {flutter::EncodableValue("value"), |
| 218 | + flutter::EncodableValue(position)}}; |
| 219 | + channel->InvokeMethod( |
| 220 | + "audio.onCurrentPosition", |
| 221 | + std::make_unique<flutter::EncodableValue>(positionWrapped)); |
214 | 222 | };
|
215 | 223 |
|
216 | 224 | PlayCompletedListener play_completed_listener =
|
@@ -242,59 +250,6 @@ class AudioplayersTizenPlugin : public flutter::Plugin {
|
242 | 250 | return audio_players_[player_id].get();
|
243 | 251 | }
|
244 | 252 |
|
245 |
| - static void StartPositionUpdates(void *data) { |
246 |
| - AudioplayersTizenPlugin *plugin = (AudioplayersTizenPlugin *)data; |
247 |
| - if (!plugin->timer_) { |
248 |
| - plugin->timer_ = ecore_timer_add(kTimeInterval, UpdatePosition, data); |
249 |
| - if (plugin->timer_ == nullptr) { |
250 |
| - LOG_ERROR("failed to add timer for UpdatePosition"); |
251 |
| - } |
252 |
| - } |
253 |
| - } |
254 |
| - |
255 |
| - static Eina_Bool UpdatePosition(void *data) { |
256 |
| - AudioplayersTizenPlugin *plugin = (AudioplayersTizenPlugin *)data; |
257 |
| - bool none_playing = true; |
258 |
| - auto iter = plugin->audio_players_.begin(); |
259 |
| - while (iter != plugin->audio_players_.end()) { |
260 |
| - try { |
261 |
| - std::string player_id = iter->second->GetPlayerId(); |
262 |
| - if (iter->second->IsPlaying()) { |
263 |
| - none_playing = false; |
264 |
| - flutter::EncodableMap duration = { |
265 |
| - {flutter::EncodableValue("playerId"), |
266 |
| - flutter::EncodableValue(player_id)}, |
267 |
| - {flutter::EncodableValue("value"), |
268 |
| - flutter::EncodableValue(iter->second->GetDuration())}}; |
269 |
| - plugin->channel_->InvokeMethod( |
270 |
| - "audio.onDuration", |
271 |
| - std::make_unique<flutter::EncodableValue>(duration)); |
272 |
| - |
273 |
| - flutter::EncodableMap position = { |
274 |
| - {flutter::EncodableValue("playerId"), |
275 |
| - flutter::EncodableValue(player_id)}, |
276 |
| - {flutter::EncodableValue("value"), |
277 |
| - flutter::EncodableValue(iter->second->GetCurrentPosition())}}; |
278 |
| - plugin->channel_->InvokeMethod( |
279 |
| - "audio.onCurrentPosition", |
280 |
| - std::make_unique<flutter::EncodableValue>(position)); |
281 |
| - } |
282 |
| - iter++; |
283 |
| - } catch (...) { |
284 |
| - LOG_ERROR("failed to update position for player %s", |
285 |
| - iter->second->GetPlayerId().c_str()); |
286 |
| - } |
287 |
| - } |
288 |
| - |
289 |
| - if (none_playing) { |
290 |
| - plugin->timer_ = nullptr; |
291 |
| - return ECORE_CALLBACK_CANCEL; |
292 |
| - } else { |
293 |
| - return ECORE_CALLBACK_RENEW; |
294 |
| - } |
295 |
| - } |
296 |
| - |
297 |
| - Ecore_Timer *timer_ = nullptr; |
298 | 253 | std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel_;
|
299 | 254 | std::map<std::string, std::unique_ptr<AudioPlayer>> audio_players_;
|
300 | 255 | };
|
|
0 commit comments