-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.js
61 lines (52 loc) · 1.1 KB
/
sound.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
import browser from "webextension-polyfill";
import { newId } from './utils';
class Sound {
constructor(config) {
this.id = config ? config.id : newId();
this.name = config ? config.name : '';
}
toPersistedProps() {
let obj = {
id: this.id,
name: this.name,
}
return obj;
}
async loadSrc() {
if (this.src) {
return Promise.resolve(this.src);
}
let id = `src.${this.id}`;
return browser.storage.local.get(id).then(items => {
if (id in items) {
return items[id];
}
return '';
});
}
async loadAudio() {
if (this.audio) {
return Promise.resolve(this.audio);
}
this.audio = new Audio();
this.audio.preload = true;
return this.loadSrc().then(src => {
this.audio.src = src;
});
}
play() {
if (this.audio) {
this.audio.currentTime = 0;
this.audio.play();
} else {
this.loadAudio().then(() => {
this.audio.play();
})
.catch(e => {
console.log('fail playing sound', e);
});
}
}
}
export default Sound;