Em Sta
Em Sta

Reputation: 1706

Javascript, Variable is not accesible by function

I made an phonegap application that plays an sound when the user clicks on the button:

 <button onclick="playAudio('test.mp3')">Play Some Audio</button>

But i want that the sound stops when the user makes an doubleclick, somehow my code dont works! Im an beginner in javascript, and think the i didnt put the variable = media in the right position. Here is my full code:

<body>
     <h1>Playing Audio</h1>

    <button onclick="playAudio('test.mp3')">Play Some Audio</button>
    <script type="text/javascript" src="cordova-2.4.0.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
        var playing = false;
        var media = null;


        function playAudio(src) {
            if (!playing) {
                if (device.platform == 'Android') {
                    src = '/android_asset/' + src;
                }

                var media = new Media(src);
                media.play();
                playing = true;
            } else {
                media.stop();
                playing = false;
            }
        }
    </script>
</body>

Upvotes: 0

Views: 184

Answers (2)

Em Sta
Em Sta

Reputation: 1706

Now i found the problem, you cannot use

 media = new Media (src) 

instead i had tu use

 my_Media = new Media (src)

There is an interreference with the word media! But what @Alexandre said was absolutley correct, and that brougt me to the final result! Thanks

Upvotes: 2

Alexandre Wiechers Vaz
Alexandre Wiechers Vaz

Reputation: 1617

Change the following line

var media = new Media(src);

to

media = new Media(src);

Upvotes: 2

Related Questions