Using dart:js to stream audio through the SoundCloud JavaScript API

Issue

I’m trying to write a library that will make it easer for dartisans to use the SoundCloud JavaScript SDK (http://developers.soundcloud.com/docs/api/sdks#javascript).

I’m using the ‘dart:js’ library, and
I’m only using one class to handle the proxy.

class SCproxy {
   JsObject proxy = context['SC'];
   String client_id;

SCproxy(this.client_id) {}  

initialize() {
   proxy.callMethod('initialize', [client_id]);
}
stream(String track_id){
   var track = new JsObject(proxy.callMethod('stream',[track_id]));
   print(track); // track should be the soundmanager2 object that we can call '.play' on.
}

The repo I’m hosting this from is (https://github.com/darkkiero/scproxy)

My problem occurs when I try to run my ‘stream’ method.

main() {
   SCproxy SC = new SCproxy('Your SoundCloud API client_ID');
   SC.initialize();
   SC.stream('/tracks/111477464'); 
}

When I try to grab and use the soundmanager2 object returned by the javascript ‘SC.stream’ method, the dart editor gives me this exception :

Breaking on exception: type 'ScriptElement' is not a subtype of type 'JsFunction' of 'constructor'.

I am under the impression that I should be able to get the dart JsObject for the soundmanager2 object by collecting the callback of the ‘SC.stream’, But I’m not sure how.However I could be completely misusing ‘dart:js’ and that would be helpful information as well.

Solution

You don’t seem to follow the SoundCloud JavaScript SDK documentation. Particularly for the stream method that takes a callback as parameter and doesn’t return.

The following Dart code :

context['SC'].callMethod('stream', ['/tracks/293', (sound) {
  sound.callMethod('play');
}]);

will do the same as this JS code :

SC.stream("/tracks/293", function(sound){
  sound.play();
});

You can have a look at Using JavaScript from Dart for more explanations.

Answered By – Alexandre Ardhuin

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.