monaca(phonegap)の高速な音声再生法
monacaを用いて,体の動きに合わせて音楽がなるアプリを開発しています.
現在,こちらのページの一番下のindex.htmlを参考にして, playAudio()
関数により音を出しています.
しかし,体の動きに合わせてなるようにプログラムを組んでいるものの,
playAudio()
関数の実行が遅いため,音楽の再生が体の動きについていきません.
0.5秒くらいの効果音をならせたいだけなのですが,
より早く音を鳴らせる方法をご存知の方いらっしゃいませんか?
なお,上記URLで指しているプログラムはこちらになります:
Plain Project Skeleton
<style type="text/css">
body {
background-image : url("images/omikuji-bg.png");
background-repeat: no-repeat;
background-position: center center;
background-size : 100% 100%;
background-attachment : fixed;
margin : 0;
padding : 0;
}
#hako {
width : 100%;
text-align : center;
margin : 10% 0px;
height : 80%;
position : fixed;
left : 0;
top : 0;
}
#bottombar {
position : absolute;
left : 0px;
bottom : 30px;
width : 100%;
text-align : center;
}
</style>
<script type="text/javascript" src="components/loader.js"></script>
<script type="text/javascript">
//Variables to specify the sound sources. They are used to generate Media objects.
var src1 = "koukaon1.mp3";
var src2 = "koukaon2.mp3";
var src3 = "koukaon3.mp3";
//In order to use Media objects, these variables must be declared here. Their initial values are null (empty).
var media1 = null;
var media2 = null;
var media3 = null;
//In Android, it's necessary to specify the absolute path
function getPath(){
var str = location.pathname;
var i = str.lastIndexOf('/');
return str.substring(0,i+1);
}
//Call "onDeviceReady" function when "deviceready" event of the Core Cordova Plugins completed.
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady(){
alert("Loading Core Cordova Plugins is completed");
/*
Generating objects to play the sound effect.
1st argument [getPath() + src1] indicates the path of the sound file.
2nd and 3rd arguments defines a callback function when encounter success and failure respectively.
*/
media1 = new Media(getPath() + src1, onSuccess, onError);
media2 = new Media(getPath() + src2, onSuccess, onError);
media3 = new Media(getPath() + src3, onSuccess, onError);
}
if (typeof Windows != "undefined") {
window.alert = function(s) {
new Windows.UI.Popups.MessageDialog(s).showAsync();
}
}
function omikuji (){
var dice = Math.floor(Math.random() * 3);
var image_name;
//Play a different sound effect for each result.
if (dice == 0) {
image_name = "omikuji-daikichi.png";
media1.play();
} else if (dice == 1) {
image_name = "omikuji-chuukichi.png";
media2.play();
} else {
image_name = "omikuji-hei.png";
media3.play();
}
document.getElementById("saisyo").setAttribute("style", "display : none;");
document.getElementById("kekka").src = "images/" + image_name;
document.getElementById("kekka").setAttribute("style", "display : inline;");
document.getElementById("button").src = "images/omikuji-btn-yarinaosu.png";
alert('The Fortune is out! What is the result?');
}
function onSuccess() {
console.log("playAudio():Audio Success");
}
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
</script>
</head>
<body>
<div id="hako">
<img src="images/omikuji-box.png" width="160" id="saisyo" />
<img src="" width="230" id="kekka" style="display : none;"/>
</div>
<div id="bottombar">
<img src="images/omikuji-btn-hajimeru.png" onclick="omikuji()" width="160" id="button">
</div>
</body>