Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to play an audio mp3 file in phonegap android

I am new to Phonegap (Cardova),I want to play a local mp3 file,it can play a file from url,but not a local file.My file is located in Assets --> www --> audio --> Ikhlas.mp3 , I using Jquery Mobile with multiple pages. and in each page I will have to play a different audio file with Oclick Play Button. when ever i click the button to play i got the following errors :

 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

any help will be highly appreciated. Thanks

<!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>
like image 226
Aamir Saif Avatar asked Jul 19 '13 08:07

Aamir Saif


1 Answers

Use Media instead of Audio and put this line of code

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

inside your function playStream()

like image 100
Miru Avatar answered Sep 28 '22 01:09

Miru