Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.

Commit

Permalink
Merge pull request #11 from 3ripleM/messageDuration-patch
Browse files Browse the repository at this point in the history
Message duration set to song duration
  • Loading branch information
Hamed Saeedi authored Apr 30, 2019
2 parents 8e8ac69 + 16e6281 commit 6f8a528
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 25 deletions.
72 changes: 72 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"@types/mocha": "^2.2.42"
},
"dependencies": {
"music-metadata": "^3.5.4",
"node-wav-player": "^0.1.0",
"path": "^0.12.7"
}
Expand Down
83 changes: 58 additions & 25 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from "vscode";
import * as mm from 'music-metadata';

const wavPlayer = require("node-wav-player");
const path = require("path");
Expand All @@ -13,52 +14,73 @@ export function activate(context: vscode.ExtensionContext) {
console.log(
'Congratulations, your extension "codechampion-vscode" is now active!'
);

let statusMessage: any | undefined = [];

const state = { isPlaying: false };

function playSound(winOrFail: string) {
var isWin = winOrFail === "win";
async function playSound(winOrFail: string) {
let isWin = (winOrFail === 'win');

var configs = vscode.workspace.getConfiguration();

var soundFileNameInConfig: any | undefined = " ";
soundFileNameInConfig = isWin
let configs = vscode.workspace.getConfiguration();
let soundFileNameInConfig: any | undefined = " ";
soundFileNameInConfig = isWin
? configs.get("codechampion.victorySoundConfig")
: configs.get("codechampion.failSoundConfig");
console.log('object', configs.get("codechampion.victorySoundConfig"))

var soundFileName = soundFileNameInConfig.split(" ").join("_") + ".wav";
let soundFileName =
soundFileNameInConfig.split(" ").join("_") + ".wav";


var soundFilePath = path.join(
let soundFilePath = path.join(
__dirname,
"..",
"sounds",
winOrFail,
soundFileName
);
state.isPlaying = true

let duration:number | undefined;
await mm.parseFile(soundFilePath)
.then(metadata => {
duration = metadata.format.duration;
})
.catch((err) => {
console.error(err.message);
});

state.isPlaying = true;
wavPlayer
.play({
path: soundFilePath,
sync: true
})
.then(() => {
state.isPlaying = false
state.isPlaying = false;
console.log("The wav file started to be played successfully.");
}).catch(() => {
state.isPlaying = true
})
.catch((error: any) => {
console.error(error);
state.isPlaying = true;
});
return new Promise(resolve => {
resolve(duration);
});
}

let playVictorySound = vscode.commands.registerCommand(
"extension.playVictorySound",
() => {
if (state.isPlaying) {
vscode.window.setStatusBarMessage("Stahp pushing that button!", 2000);
const message = vscode.window.setStatusBarMessage("Stahp pushing that button!", 2000);
statusMessage.push(message);
} else {
vscode.window.setStatusBarMessage("Congratulations!", 2000);

playSound("win");
playSound('win')
.then((duration: any) => {
const message = vscode.window.setStatusBarMessage("Congratulations!", duration * 1000);
statusMessage.push(message);
});
}
}
);
Expand All @@ -67,19 +89,30 @@ export function activate(context: vscode.ExtensionContext) {
"extension.playFailSound",
() => {
if (state.isPlaying) {
vscode.window.setStatusBarMessage("Stahp pushing that button!", 2000);
const message = vscode.window.setStatusBarMessage("Stahp pushing that button!", 2000);
statusMessage.push(message);
} else {
vscode.window.setStatusBarMessage("It's Ok, Don't worry!", 2000);

playSound("fail");
playSound('fail')
.then((duration: any) => {
console.log(duration);
const message = vscode.window.setStatusBarMessage("It's Ok, Don't worry!", duration * 1000);
statusMessage.push(message);
});
}
}
);

let stopSound = vscode.commands.registerCommand("extension.stopSound", () => {
wavPlayer.stop();
state.isPlaying = false
});
let stopSound = vscode.commands.registerCommand(
"extension.stopSound",
() => {
wavPlayer.stop();
state.isPlaying = false;
statusMessage.forEach((message: any) => {
message.dispose();
});
}
);


context.subscriptions.push(playVictorySound);
context.subscriptions.push(playFailSound);
Expand Down

0 comments on commit 6f8a528

Please sign in to comment.