6

我是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>
4

1 回答 1

3

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

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

在你的函数里面playStream()

于 2013-07-19T10:39:29.847 回答