0

I had created my Android App as it play sound when change select input option to on. I had created my interface to call android method in html . I did my code well but when I change the option value to on it doesn't work .

Play sound method:

public void playsound(String value ) {
    if (value == "on") {
    mp= MediaPlayer.create(mContext,R.raw.sound);

    /* mp.prepare();*/
    mp.start();
    }
}

<form >       
      <select name="flip-1" id="flip-1" data-role="slider" >
      <option value="off">Off</option>
      <option value="on" onclick="playmp3('on')">On</option>
      </select>

 <script type="text/javascript">
       function playmp3(var value) {
       Android.playsound(value);
       }  
</script>

</form >

egydeveloper
  • 585
  • 5
  • 7
  • 27

3 Answers3

0

try this:

if (value.equals("on")) {
mp= MediaPlayer.create(mContext,R.raw.sound);

/* mp.prepare();*/
mp.start();
}

Howto call Android method from JS take a look to this link:

Call Android method from JS

Community
  • 1
  • 1
nano_nano
  • 12,351
  • 8
  • 55
  • 83
  • it doesn't worked also i tried to call the method without value but it didn't work – egydeveloper Jun 24 '13 at 10:01
  • Is playSound part of the JavaScriptInterface? Anyhow... value == "on" cannot work. You have to change it to equals("on"). With == you compare the object reference not the value of your string. – nano_nano Jun 24 '13 at 10:05
0

I think you should use the onchange event in the select element instead of the onclick event in the option element.

<select name="flip-1" id="flip-1" data-role="slider" onchange="f1()">
      <option value="off">Off</option>
      <option value="on" onclick="playmp3('on')">On</option>
</select>

<script type="text/javascript">
    function f1(){
      var myselect = document.getElementById("flip-1");
      playmp3(myselect.options[myselect.selectedIndex].value);
    }
</script>

Also, you have an error in your javascript method, the var keyword is not necessary:

 function playmp3(value) {
       Android.playsound(value);
 } 
Jonathan Naguin
  • 14,526
  • 6
  • 46
  • 75
  • it worked but the default value "on" only it will work , i need the user will selected – egydeveloper Jun 24 '13 at 10:47
  • @egydeveloper What do you mean? The listener function has to check the value of the inner option and call the `playmp3`. I have updated my answer to reflect this behaviour... – Jonathan Naguin Jun 24 '13 at 10:56
0
if (value.equals("on"))
{
   //Your code
}
Kostya Khuta
  • 1,846
  • 6
  • 26
  • 48