无法在phonegap android中播放音频mp3文件

时间:2013-07-19 08:08:05

标签: javascript html jquery-mobile cordova

我是Phonegap(Cardova)的新手,我想播放本地mp3文件,它可以播放来自url的文件,但不能播放本地文件。我的文件位于Assets --> www --> audio --> Ikhlas.mp3,我将Jquery Mobile用于多个页面。在每个页面中,我将不得不使用Oclick播放按钮播放不同的音频文件。当我点击按钮播放时,我得到以下错误

 07-19 07:43:58.829: E/MediaPlayer(1081): Attempt to call getDuration without a valid mediaplayer,
07-19 07:43:58.838: E/MediaPlayer(1081): Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=0

任何帮助都将受到高度赞赏。感谢

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title></title>

<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>
<link rel="stylesheet"  href="css/themes/default/jquery.mobile-1.3.1.min.css">
<link rel="stylesheet" href="_assets/css/jqm-demos.css">
<link rel="shortcut icon" href="favicon.ico">
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,700">
<link rel="stylesheet" href="css/themes/default/my_style.css">
<link rel="stylesheet" media="screen" href="http://openfontlibrary.org/face/droid-arabic-naskh" rel="stylesheet" type="text/css"/>
<script src="js/jquery.js"></script>
<script src="_assets/js/index.js"></script>
<script src="js/jquery.mobile-1.3.1.min.js"></script>


<script>
var myaudio = new Audio('/android_asset/www/audio/Ikhlas.mp3');
function playStream() {
  try {
   //alert ("ffff");

    myaudio.id = 'playerMyAdio';
    myaudio.play();
  } catch (e) {
    alert('no audio support!');
  } 
}


function stopStream() {
  try {
   myaudio.pause();
  } catch (e) {
    alert('no audio support!');
  } 
}
</script> 
<div data-role="page" id="Ikhlas">
<div data-role="header">
<a href="#page2" class="ui-btn-left" data-icon="back" data-iconpos="notext" class="ui-   icon-nodisc" data-iconshadow="false" data-transition="slide">back</a>
<a href="index.html" data-icon="home" class="ui-btn-right" class="ui-icon-nodisc"
data-iconshadow="false" data-iconpos="notext">Home</a>  
</div>
<!-- /header -->
<body>
<div data-role="content">
<button onClick="playStream()">play</button><br />
<button onClick="stopStream()">stop</button><br />
</div>
<!-- /content -->
<div data-role="footer"><a href="#page2" data-role="button" data-icon="back"      data-transition="slide">Go Back</a></div>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

使用Media而不是Audio 并把这行代码

var myaudio = new Media('/android_asset/www/audio/Ikhlas.mp3');

在你的函数playStream()