我是 Android 和 Phonegap 的新手。我想使用最新版本的 Phonegap 创建一个简单的 MP3 播放器。我已经严格按照教程进行操作,并且已经安装并测试了 Phonegap 的插件。一半时间,错误 fxn 被媒体 fxn 调用。其余时间,什么都没有发生。我究竟做错了什么?
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/jquery.mobile.structure-1.3.2.css" />
<link rel="stylesheet" type="text/css" href="css/jquery.mobile.theme-1.3.2.css" />
<link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.3.2.css" />
<title>Player</title>
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript" src="js/jquery-1.10.1.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.3.2.js"></script>
<script type="text/javascript">
// Audio player
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
playAudio("file:///android_asset/www/test.wav");
// var path = window.location.pathname;
// path = path.substr( path, path.length - 10 );
// alert('file://' + path);
}
//
var my_media = null;
var mediaTimer = null;
function playAudio(src) {
if (my_media == null) {
// Create Media object from src
my_media = new Media(src, onSuccess, onError);
} // else play current audio
// Play audio
my_media.play();
// Update my_media position every second
if (mediaTimer == null) {
mediaTimer = setInterval(function() {
// get my_media position
my_media.getCurrentPosition(
// success callback
function(position) {
if (position > -1) {
setAudioPosition((position) + " sec");
}
},
// error callback
function(e) {
console.log("Error getting pos=" + e);
setAudioPosition("Error: " + e);
}
);
}, 1000);
}
}
// Pause audio
//
function pauseAudio() {
if (my_media) {
my_media.pause();
}
}
// Stop audio
//
function stopAudio() {
if (my_media) {
my_media.stop();
}
clearInterval(mediaTimer);
mediaTimer = null;
}
// onSuccess Callback
//
function onSuccess() {
console.log("playAudio():Audio Success");
}
// onError Callback
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// Set audio position
//
function setAudioPosition(position) {
document.getElementById('audio_position').innerHTML = position;
}
</script>
</head>
<body>
<div id="home" data-role="page" data-theme="b">
<div data-role="header">
<h1>Home</h1>
</div>
<div data-role="content">
<p style="text-align:center;">MP3 Player:</p>
<div data-role="controlgroup">
<a onclick="playAudio('file:///android_asset/www/audio/tailtoddle.mp3');" data-role="button" data-icon="arrow-r" data-iconpos="right">Play sound</a>
</div>
</div>
</div>
</body>