shime / play-sound

Play sounds by shelling out to one of the available audio players.
MIT License
207 stars 31 forks source link

Audio doesn't play when play function is inside other callbacks (using bleno library) #41

Open federicomazzini opened 4 years ago

federicomazzini commented 4 years ago

Good afternoon and thank you for your work.

I'm trying to play an audio file when I receive a value from a bluetooth characteristic on a Raspberry Pi. I'm using bleno for bluetooth. I can play files following the example, but I cannot play them when inside the bluetooth loop and I don't know how to debug that. I'm not sure if it's because of being inside another process or because of something related with the event loop, or maybe because of the path of the file, but it does not throw an error. Also I'm using a setTimeout function with a number coming from bluetooth, but I've done that before without any issues.

This is the file in which I try to play an mp3 file:

const player = require('play-sound')();
module.exports.squeduleSound = function (seconds) {
    setTimeout(function() {
    playSound();
    }, seconds*1000);
};

function playSound() {
        console.log('Playing sound');
        player.play('./sound.mp3', (err) => {
                if (err) console.log('Could not play sound: ${err}');
    });
}

And this is the bluetooth callback, the onWriteRequest method is part of the bleno library:

var soundSqueduler = require('../audioPlayer/audioScheduler');
WriteTimeCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
    this._value = data;
        var seconds =  this._value.toString('utf8');
        console.log('CustomCharacteristic - onWriteRequest: value = ' + seconds);
        soundSqueduler.squeduleSound(seconds);
    callback(this.RESULT_SUCCESS);
};

I'm using node version: 8.11.1. (I can't use a newer one because of bleno. But I've done this in the past with this version.) bleno version: 0.5.0 play-sound: 1.1.3

Thank you