Skip to content

Commit

Permalink
Merge pull request #113 from alnitak/loadFile_freeze
Browse files Browse the repository at this point in the history
using `loadFile` or `loadMem` freeze the UI
  • Loading branch information
alnitak authored Aug 2, 2024
2 parents 46c6f4b + 377852e commit 99bb7f9
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 166 deletions.
28 changes: 25 additions & 3 deletions lib/src/soloud.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';

@pragma('vm:entry-point')
void _loadFile(Map<String, dynamic> args) {
SoLoudController().soLoudFFI.loadFile(
args['path'] as String,
LoadMode.values[args['mode'] as int],
);
}

@pragma('vm:entry-point')
({PlayerErrors error, SoundHash soundHash}) _loadMem(
Map<String, dynamic> args) {
return SoLoudController().soLoudFFI.loadMem(
args['path'] as String,
args['buffer'] as Uint8List,
LoadMode.values[args['mode'] as int],
);
}

/// The main class to call all the audio methods that play sounds.
///
/// This class has a singleton [instance] which represents the (also singleton)
Expand Down Expand Up @@ -420,13 +438,13 @@ interface class SoLoud {
throw const SoLoudNotInitializedException();
}

_controller.soLoudFFI.loadFile(path, mode);

final completer = Completer<AudioSource>();
loadedFileCompleters.addAll({
path: completer,
});

await compute(_loadFile, {'path': path, 'mode': mode.index});

return completer.future.whenComplete(() {
loadedFileCompleters.removeWhere((key, __) => key == path);
});
Expand Down Expand Up @@ -470,7 +488,11 @@ interface class SoLoud {
path: completer,
});

final ret = _controller.soLoudFFI.loadMem(path, buffer, mode);
final ret = await compute(_loadMem, {
'path': path,
'buffer': buffer,
'mode': mode.index,
});

/// There is not a callback in cpp that is supposed to add the
/// "load file event". Manually send this event to have only one
Expand Down
8 changes: 4 additions & 4 deletions src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ extern "C"

Player *p = player.get();
unsigned int hash = 0;
// std::thread loadThread([p, completeFileName, loadIntoMem, hash]()
// std::thread loadThread([p, pa, completeFileName, loadIntoMem, hash]()
// {
PlayerErrors error = p->loadFile(pa.string(), loadIntoMem, &hash);
PlayerErrors error = p->loadFile(pa.string(), loadIntoMem, (unsigned int*)&hash);
// printf("*** LOAD FILE FROM THREAD error: %d hash: %u\n", error, *hash);
fileLoadedCallback(error, completeFileName, &hash);
// });
fileLoadedCallback(error, completeFileName, (unsigned int*)&hash);
// });
// // TODO(marco): use .detach()? Use std::atomic somewhere
// loadThread.join();
}
Expand Down
Binary file modified web/libflutter_soloud_plugin.wasm
Binary file not shown.
Loading

0 comments on commit 99bb7f9

Please sign in to comment.