我的代码非常简单:
<script type="text/javascript" charset="utf-8">
function playAudio() {
// i try to specificate path to audio file different ways:
var src ="/android_asset/www/track.mp3";
//src = "http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3";
var media = new Media(src, // success callback
function() {
console.log("playAudio():Audio Success");
},
// error callback
function(err) {
console.log("playAudio():Audio Error: "+err);
});
alert('sm'); // alert doesn't work -> so prev func 'new Media' doesn't work too
media.play();
}
</script>
这个函数的调用是这样的:
<body onload="playAudio()">
我不明白 - 为什么不起作用?音频文件是'assets / www / track.mp3'
答案 0 :(得分:0)
问题出在这里
var src ="/android_asset/www/track.mp3";
JS不知道/android_asset/
只有JAVA才知道这条路径,所以要么使用java-JS注入,要么将此文件放在www文件夹中。
答案 1 :(得分:0)
我还不明白为什么,但是当我尝试使用此代码时:
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
function playAudio(url) {
// Play the audio file at url
// http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3
var my_media = new Media(url,
// success callback
function () {
console.log("playAudio():Audio Success");
alert("ok");
},
// error callback
function (err) {
console.log("playAudio():Audio Error: " + err);
alert("oops");
}
);
// Play audio
my_media.play();
}
</script>
并称之为:
<a href="#" class="btn large" onclick="playAudio('/android_asset/www/track.mp3');">Play Audio</a>
或者当我这样称呼它时:
<a href="#" class="btn large" onclick="playAudio('http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3');">Play Audio</a>
只在模拟器上才有效!但是在网络浏览器中它仍然无法正常工作