2016-12-08 7 views
0

私は3時間以上ウェブを検索して、オーディオファイルを再生する方法を探していましたが、残念ながら役に立たないものは見つかりませんでした。私はいくつかのタスクを自動化するCasperJSを持っており、すべてのタスクを完了した後にオーディオファイル(例えばbeep.wav)を再生したいと思っていました。私はそれが可能かどうか疑問に思います。PhantomJS/CasperJSでサウンドを再生する方法は何ですか?

casper.run(function(casper) { 
    fs.write(saveDir, JSON.stringify(content, null, ' '), 'w'); 
    // play an audio file before exiting.... 
    casper.exit(); 
}); 
+1

'PJS'は窓がありませんが.Audio関数、おそらくあなたは 'SlimerJS'を使用する必要があります –

+1

うーん...面白いです。はい、提案していただきありがとうございます。私はそれの周りに他の方法がない場合、それを探索しようとします:) – Pennf0lio

答えて

4

Child Process Moduleを使用して、スクリプトを実行して音楽を再生する必要があります。

私は音楽再生するにはpl.shスクリプトを作成しました:

var casper = require('casper').create(); 
casper 
.start('http://domu-test-2/node/12', function(){ 
    var childProcess; 
    try { 
     childProcess = require("child_process") 
    } catch(e){ 
     console.log(e, "(error)") 
    } 
    if (childProcess){ 
     childProcess.execFile("/bin/bash", ["./pl.sh"], null, function(err, stdout, stderr){ 
      console.log("execFileSTDOUT: "+stdout); 
      console.log("execFileSTDERR:",stderr); 
     }); 
     console.log("Shell commands executed") 
    } else { 
     console.log("Unable to require child process (error)") 
    } 
    this.wait(10000)// need to wait to play the sound 
}) 
.run(); 

そしてスクリプトPhantomJS:その後、私はCasperJSスクリプトを作成し

#!/bin/bash 
mplayer "/music/downloads2/Technoboy - Into Deep.oga" 
exit 0 

var page = require('webpage').create(); 
page.open('http://domu-test-2/node/12', function() { 
    var childProcess; 
    try { 
     childProcess = require("child_process") 
    } catch(e){ 
     console.log(e, "(error)") 
    } 
    if (childProcess){ 
     childProcess.execFile("/bin/bash", ["./pl.sh"], null, function(err, stdout, stderr){ 
      console.log("execFileSTDOUT: "+stdout); 
      console.log("execFileSTDERR:",stderr); 
     }); 
     console.log("Shell commands executed") 
    } else { 
     console.log("Unable to require child process (error)") 
    } 

    setTimeout(phantom.exit,10000)// need to wait to play the sound 
}); 
+1

ありがとう@Igor!これは魅力的に機能しました!私はOSXシステム上にいるので、bplスクリプトを変更して 'mplayer'の代わりに 'afplay'を使用する必要がありました。 – Pennf0lio

関連する問題