[Flashcards]update Flashcards(tizen_2.1)
[samples/web/Flashcards.git] / js / sound.js
1 /*
2  * Copyright (c) 2012, Intel Corporation.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 function GameSound(id, src, preload, loop) {
11     var audioElement = document.createElement('audio');
12     audioElement.setAttribute("id", id);
13     audioElement.setAttribute("src", src);
14     audioElement.setAttribute("preload", preload);
15     if (loop)
16         audioElement.setAttribute("loop", "loop");
17     document.body.appendChild(audioElement);
18
19     var me = this;
20     this.id = id;
21     this.soundobj = document.getElementById(id);
22     this.enable = false;
23     this.infocus = true;
24     this.loop = (loop == undefined)?false:loop;
25     this.focus = function focus() {
26         if(!me.infocus)
27         {
28             me.infocus = true;
29             if(me.enable&&me.loop)
30                 me.soundobj.play();
31         }
32     };
33     this.blur = function blur() {
34         if(me.infocus)
35         {
36             me.infocus = false;
37             if(me.enable&&me.loop)
38                 me.soundobj.pause();
39         }
40     };
41     window.addEventListener('focus', me.focus, false);
42     window.addEventListener('blur', me.blur, false);
43
44     this.play = function play() {
45         this.enable = true;
46         if(this.infocus)
47             this.soundobj.play();
48     };
49     this.pause = function pause() {
50         this.enable = false;
51         this.soundobj.pause();
52     };
53 }