# How do I get my core to play an MP3?

**URL:** <https://community.particle.io/t/how-do-i-get-my-core-to-play-an-mp3/11509>\
**Category:** General\
**Created:** [April 29, 2015, 4:32pm UTC](https://community.particle.io/t/how-do-i-get-my-core-to-play-an-mp3/11509 "2015-04-29T16:32:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skrotseng](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@skrotseng](https://community.particle.io/u/skrotseng)\
**Post date:** [April 29, 2015, 4:32pm UTC](https://community.particle.io/t/how-do-i-get-my-core-to-play-an-mp3/11509/1 "2015-04-29T16:32:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rsteckler](https://avatars.discourse-cdn.com/v4/letter/r/e495f1/32.png) [@rsteckler](https://community.particle.io/u/rsteckler)\
**Post date:** [April 29, 2015, 6:33pm UTC](https://community.particle.io/t/how-do-i-get-my-core-to-play-an-mp3/11509/2 "2015-04-29T18:33:36Z")

</div>

I’m using a VS1053 shield from adafruit: [https://www.adafruit.com/products/1381](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](https://github.com/McTristan/Adafruit_VS1053_Library)

It works a treat!

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [April 29, 2015, 6:42pm UTC](https://community.particle.io/t/how-do-i-get-my-core-to-play-an-mp3/11509/3 "2015-04-29T18:42:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![skrotseng](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@skrotseng](https://community.particle.io/u/skrotseng)\
**Post date:** [May 2, 2015, 9:09pm UTC](https://community.particle.io/t/how-do-i-get-my-core-to-play-an-mp3/11509/4 "2015-05-02T21:09:32Z")

</div>

Hi Harrison,

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

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [May 4, 2015, 7:09am UTC](https://community.particle.io/t/how-do-i-get-my-core-to-play-an-mp3/11509/5 "2015-05-04T07:09:42Z")

</div>

Here’s a brief starter:

Playing sounds/music: [https://www.npmjs.com/package/play-audio](https://www.npmjs.com/package/play-audio)

Spark’s Node API Wrapper: [https://github.com/spark/sparkjs](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);
});
```
