How do I get my core to play an MP3?

Hi Everyone -

I’m working on a small project, and I have limited experience with programming and the Spark system. I’m hooking up my Spark core to two ultrasonic rangefinders, pointed in different directions. When the rangefinder detects something, I want a corresponding MP3 to play from the computer speakers that I have. Each rangefinder would trigger a different MP3.

I am fine with the MP3s playing from my laptop. I can put the files in the library for the .ino that I’m going to write, if need be. I was just wondering if anyone had any experience with something like this.

-Stephen

I’m using a VS1053 shield from adafruit: https://www.adafruit.com/products/1381

and the ported library from McTristan (a fork of the original port by Paul Kourany (I think)): https://github.com/McTristan/Adafruit_VS1053_Library

It works a treat!

If you don’t mind playing the sounds from your laptop then a Node.js app which listening for two different Spark Events and then plays the MP3s would work for you! You’d have an app running on your laptop and some code on your Spark Core pushing out events

Hi Harrison,

That sounds promising. Do you have any more specifics for a beginner?

Here’s a brief starter:

Playing sounds/music: https://www.npmjs.com/package/play-audio

Spark’s Node API Wrapper: https://github.com/spark/sparkjs

Some Psuedo Code:
On Core:
Spark.publish("SND_PLAY")

On Node (something LIKE this. This is untested and probably won’t even compile but should give you the right idea)

var spark = require('spark');
play = require('play-audio');



//example code to re open the SSE stream when it ends
 var openStream = function() {

    //Get your event stream
    var req = spark.getEventStream(false, 'mine', function(data) {
      console.log("Event: " + JSON.stringify(data));

      var eventName = data['eventName'] // This is probably wrong. Use the above console.log to figure out the correct array key
      if (eventName == "SND_PLAY")
      {
        play('song.mp3').play();
      }

    });

    req.on('end', function() {
        console.log("ended!  re-opening in 1 second...");
        setTimeout(openStream, 1000);
    });
};

spark.on('login', function() {
    openStream();
});


spark.login({ username: 'email@example.com', password: 'password' }, function(err, body) {
  console.log('API call login completed on callback:', body);
});