Search code examples
javascripttizentizen-web-apptizen-wearable-sdk

Tizen web app not playing multiple custom audios


I'm looking to use multiple audio files in a tizen wearable web app. It works for 1 audio but no matter what i try it doesn't work for multiple.

I've tried declaring multiple audio files and then passing them as a variable to a function with no luck.

var sound1 = new Audio("audio/first_file.wav");
sound1.loop = false;
sound1.volume = 15.0;

var sound2 = new Audio("audio/second_file.wav");
sound2.loop = false;
sound2.volume = 15.0;

function startSample(sound) {
    sound.play();
    tizen.feedback.stop();
}

I've tried declaring the audio file within the function

var sound1 = "audio/first_file.wav";
var sound2 = "audio/second_file.wav";

function startSample(sound) {
    var soundSample = new Audio(sound);
    soundSample.loop = false;
    soundSample.volume = 15.0;
    sound.play();
    tizen.feedback.stop();
}

I even tried giving them separate functions, but it seems even with only one audio, declaring the audio variable within a function does not work.

function soundSample1() {
    var sound1 = new Audio("audio/first_file.wav");
    sound1.loop = false;
    sound1.volume = 15.0;
    sound.play();
    tizen.feedback.stop();
}

The one thing that does work is this, but only when there's only one audio file.

var sound1 = new Audio("audio/first_file.wav");
sound1.loop = false;
sound1.volume = 15.0;

function startSample() {
    sound1.play();
    tizen.feedback.stop();
}

Any help would be greatly appreciated.


Solution

  • After some further experimentation I ended up with this;

    <audio id="myAudio">
        <source src="audio/first_file.wav" type="audio/wav">
    </audio>
    <audio id="myAudio2">
        <source src="audio/second_file.wav" type="audio/wav">
    </audio>
    <script>
        var sound1 = document.getElementById("myAudio");
        var sound2 = document.getElementById("myAudio2");
        function startSample(sound) {
            sound.play();
        }
    </script>
    

    Credit to w3schools for the original code which was modified from here https://www.w3schools.com/jsref/met_audio_play.asp