0

I'm creating an android web app and it's simple thing. I have a button and I want it to play a sound when clicked. I'v searched a lot and actually no proper solution that could help me out.

I'm a newbie on this part and I really have no idea how I can play my sound.

any help will be appreciated.

Edited : I installed media api on cordova : var media = new Media(src, mediaSuccess, [mediaError], [mediaStatus]);

this is my html part :

<a href="#" id="applaud"><img src="images/applaud.png"></a></li>

this is the javascript part :

$("#applaud").bind('tap', function() {
            var media = new Media("lib/sounds/hooray.mp3", mediaSuccess);
            media.play();
        });

BTW I'm using jQuery Mobile for this application. Should I do anything else.

Thanks to those who answered my questions.

Update : I tried this code too ->

function playAudio(id) {
        var audioElement = document.getElementById(id);
        var url = audioElement.getAttribute('src');
        var my_media = new Media(url,
                // success callback
                 function () { alert("success"); },
                // error callback
                 function (err) { alert("error"); }
        );
               // Play audio
        my_media.play();
    }

and then in my html it will be like this :

<a href="#" id="applaud" onclick="playAudio('/android_asset/www/lib/sounds/hooray.mp3');"><img src="images/applaud.png"></a>

still nothing, I even revert my cordova to version 3.1 but nothing guys I really need help.

bhrzk
  • 43
  • 6
  • 1
    You need to provide more information. You mentioned in another comment that you are using Cordova. What have you tried? What does your code look like? – T Percival Jun 06 '14 at 16:11
  • Did you installed the media plugin correctly? and used as documentation instructed? Provide some of your main code. – AtanuCSE Jun 06 '14 at 18:20
  • I added some codes on my first post. – bhrzk Jun 07 '14 at 07:57
  • @AtanuCSE do you have any manual for installing and using this plugin ? (except the one is on the phonegap website) – bhrzk Jun 07 '14 at 15:21
  • @Voicu could you help me outta here ? – bhrzk Jun 07 '14 at 18:54
  • nope I follwed the same documentation. One thing I didn't understand ` onclick="playAudio('/android_asset/www/lib/sounds/hooray.mp3');"` here you are sending the URL, and in method declaration you're catching ID!! `function playAudio(id)` the id has to be the url – AtanuCSE Jun 08 '14 at 15:40

1 Answers1

0

It sounds like you're developing an HTML web app. Try the HTML5 audio tag.

T Percival
  • 8,526
  • 3
  • 43
  • 43
  • Thanks for quick answer , I tried this but when I build the app with cordova , the sounds wont play – bhrzk Jun 06 '14 at 16:09