LoginSignup
0
1

More than 3 years have passed since last update.

WSLのUbuntuでPC内の音楽データをGoogleHomeで操作する(shuffle再生)

Last updated at Posted at 2019-09-09

はじめに

「WSLのUbuntuでPC内の音楽データをGoogleHomeで操作する」のshuffle再生バージョンです。
【追加】2020-5-1
playlist作成とmplayerの再生で同期が取れず、playlistが未作成で再生しようとするので、
setTimeoutで遅延をいれました。

プログラム(node.js)

●grep検索で抽出した音楽データのプレイリストを作成する。
●mplayerのオプションでshuffle再生する。(mplayer -shuffle -playlist)
●音楽データのリストを予めフリーソフトで作成してください。(list.txt)
/mnt/j/music/松任谷由実/日本の恋と、ユーミン/01 やさしさに包まれたな.mp3
/mnt/j/music/松任谷由実/日本の恋と、ユーミン/01 リフレインが叫んでる.mp3
/mnt/j/music/松任谷由実/日本の恋と、ユーミン/01 真珠のピアス.mp3
ダブルクオーテーションは不要です。

var firebase = require("/home/ユーザ名/node_modules/firebase");
require("/home/ユーザ名/node_modules/firebase");
var iconv = require('/home/ユーザ名/node_modules/iconv-lite');
var value1 ;

//firebase config
var config = {
    apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    authDomain: "xxxxxxxxxx.firebaseapp.com",
    databaseURL: "https://xxxxxxxxxx.firebaseio.com",
    projectId: "xxxxxxxxxx",
    storageBucket: "xxxxxxxxxx.appspot.com",
    messagingSenderId: "xxxxxxxxxxxxxx"
};

function sleep(time) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, time);
  });
}

firebase.initializeApp(config);

//jsonからvalueに一致する値取得
const getJsonData = (value, json) => {
  for (var word in json)  if (value == word) return json[word]
  return json["default"]
}

//database更新時
const path = "/googlehome5"
const key = "word"
const db = firebase.database()
db.ref(path).on("value", function(changedSnapshot) {
//値取得
let value = changedSnapshot.child(key).val()
if (!value) return
console.log(value)
var value1 = value.replace(/ /g,"");

//コマンド
const STOP = "pkill mplayer";

if(value === "停止"){
  command = STOP
} else if(value === "止め て"){
  command = STOP
} else {
  command = "未定義" 
}
console.log(command)

//コマンド実行
var exec = require('child_process').exec;
exec(STOP, {maxBuffer: 1000*1024}, function(error, stdout, stderr) {
  if (error !== null) {
    console.log('Exec error: ' + error);
  }
});
sleep(2000);
if(command === "未定義") {
  command = 'cat /mnt/j/music/list.txt | grep "'  + value1 + '"';
  var exec = require('child_process').exec;
  exec(command, {maxBuffer: 40000*1024}, function(error, stdout, stderr) {
    if (error !== null) {
      console.log('指定された曲はありません');
    } else {
      console.log(stdout);
    }
  });

  command = 'cat /mnt/j/music/list.txt | grep "'  + value1 + '">/mnt/j/music/playlist';
  console.log(command)
  var exec = require('child_process').exec;
  exec(command, {maxBuffer: 40000*1024}, function(error, stdout, stderr) {
    if (error !== null) {
      console.log('Exec error: ' + error);
    }
  });
  function mplay(){
  command = 'mplayer -shuffle -playlist /mnt/j/music/playlist';
  console.log(command)
  var exec = require('child_process').exec;
  exec(command, {maxBuffer: 40000*1024}, function(error, stdout, stderr) {
    if (error !== null) {
      console.log('Exec error: ' + error);
    }
  });
  }
  setTimeout(mplay,3000);

}
//firebase clear
db.ref(path).set({[key]: ""});
})
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1