0
votes

I have created regular Dart utility class SoundLibrary, that I would like to make more AngularDart'ish. The utility class can be used to load some sound files, and afterwards play parts of the files.

The (async) method used to fetch the sound files is:

Future<Null> fetchAudioFiles(List<String> audioFiles) { ... }

And a method for playing a specified part of one of the retrieved sounds is:

playSound(String audioFile, double startTime, double duration) { ... }

A simple use case is:

main() {
    SoundLibrary sb = new SoundLibrary();
    sb.fetchAudioFiles(["explosions.mp3", "engines.mp3"]).then((_){
        sb.playSounds("explosions.mp3", 5.34, 2.1);
    });
}

I would like AngularDart to create a "service instance" of this class, that I can inject into other instances. I would also like AngularDart to wait for the instance to be fully ready (all sounds are loaded) before AngularDart actually starts my app (as I don't want delays when playing a sound from a given file for the first time).

How can I achieve this, in an AngularDart'ish way, so AngularDart PRELOADs the sounds, while AngularDart itself initializes. Something like: while AngularDart initializes also invoke soundLibrary.fetchAudioFiles(..). Then wait for AngularDart to initialize AND all sounds to have been loaded. After initialization finishes, I would expect, that all controllers that has gotten the SoundLibrary injected, can call sb.playsound(..) WITHOUT worrying, that the sounds have not finished loading yet.

2

2 Answers

1
votes

Just put the ngBootstrap() call into the fetchAudioFiles() then() call:

main() {
  SoundLibrary sb = new SoundLibrary();
  sb.fetchAudioFiles(["explosions.mp3", "engines.mp3"]).then((_){
    ngBootstrap(module : new myModule());
  }); 
}

You could also try registering your SoundLibrary with a value:

class myModule extends Module {
   value(SoundLibrary, new SoundLibrary()
                         ..fetchAudioFilesSync(["explosions.mp3", "engines.mp3"]));
}

With fetchAudioFilesSync being a synchronous operation.

0
votes

You just create a class and register it in your module

type(SoundLibrary);

you inject it to your controller/component/directive

class MyController {
  SoundLibrary _sl;
  MyController(this._sl) {
    _sl.fetchAudioFiles().then(sb.playSound('explosions.mp3', 5.34, 2.1);
  }
}

To elaborate on more details, you need to provide more information.
There isn't much AngularDartish in calling a method and waiting until it returns.