shime / play-sound

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

Windows looping sound? #19

Open Klemen1337 opened 7 years ago

Klemen1337 commented 7 years ago

When I play a sound it keeps looping and it never ends... Dose anyone have the same problem?

var player = require('play-sound')(opts = {player: "mplayer"})
player.play("test.wav", function(err){
  if (err){
    console.error(err);
  }
})
Waog commented 6 years ago

same problem here, but it only happens with some of my mp3 files, some files work just fine.

Also mplayer itself is not the problem... when i run mplayer my.mp3 everything works fine with all files.

Isht commented 5 years ago

Wow, I thought something wrong with my code. It's happen after I set timeout. Do you guys solve this problem?

Waog commented 5 years ago

@Isht nope, I built some nasty workaround, which reads the length of the audio file and stops playing the file after that time.

Waog commented 5 years ago

Here's my workaround code

// TODO remove this:
let opts;

import { AudioPlayer } from "../audio-player";
var player = require("play-sound")(
  (opts = { player: "./mplayer/mplayer.exe" })
);
var mp3Duration = require("mp3-duration");
import { promisify } from "util";
import sleep from "../../util/sleep";

// TODO: Promisify or totally replace the audio player
export class Mp3Player implements AudioPlayer {
  async play(filePath: string): Promise<void> {
    const getDuration = promisify(mp3Duration);
    const duration = await getDuration(filePath); // audio duration in seconds

    let audio = player.play(
      filePath,
      { "./mplayer/mplayer.exe": ["-ao", "sdl"] },
      (err: Error) => {
        if (err) throw err;
      }
    );

    await sleep(duration * 1500); // workaround's workaround, because killing the audio exactly after it's length is to early (probably because it only starts playing after some delay)
    audio.kill();
  }
}
kqvanity commented 2 years ago

@Waog This question might be unrelated to the thread, but how come are you using both require and import statements, without importing require itself (assuming that you have the type field as module at the package.json file)