|
67 | 67 | } |
68 | 68 |
|
69 | 69 | setPath(path) { |
70 | | - this.mAudioBlobPromise = fetch(path); |
| 70 | + this.mAudioPath = path; |
71 | 71 | } |
72 | 72 |
|
73 | 73 | execute(next, status) { |
74 | | - this.mAudioBlobPromise.then((response) => response.blob()) |
75 | | - .then((blob) => { |
76 | | - var audioURL = window.URL.createObjectURL(blob); |
77 | | - this.mAudio = new Audio(audioURL); |
78 | | - this.mAudio.addEventListener("canplaythrough", (event) => { |
79 | | - this.mAudio.play() |
80 | | - .then(() => next()) |
81 | | - .catch((reason) => { |
82 | | - status.fail(`ERROR playing audio: ${reason}`); |
83 | | - next(); |
84 | | - }) |
| 74 | + fetch(this.mAudioPath) |
| 75 | + .catch((error) => { |
| 76 | + status.fail(`ERROR fetching audio file: ${error}`); |
| 77 | + next(); |
| 78 | + }) |
| 79 | + .then((response) => { |
| 80 | + if (response.ok) { |
| 81 | + return response.blob(); |
| 82 | + } else { |
| 83 | + throw new Error(`Bad response from fetching audio file: ${response.status}`); |
| 84 | + } |
| 85 | + }) |
| 86 | + .catch((error) => { |
| 87 | + status.fail(`ERROR getting response blob: ${error}`); |
| 88 | + next(); |
| 89 | + }) |
| 90 | + .then((blob) => { |
| 91 | + try { |
| 92 | + var audioURL = window.URL.createObjectURL(blob); |
| 93 | + this.mAudio = new Audio(audioURL); |
| 94 | + this.mAudio.addEventListener("canplaythrough", (event) => { |
| 95 | + this.mAudio.play() |
| 96 | + .then(() => next()) |
| 97 | + .catch((reason) => { |
| 98 | + status.fail(`ERROR playing audio: ${reason}`); |
| 99 | + next(); |
| 100 | + }) |
| 101 | + }); |
| 102 | + } catch (e) { |
| 103 | + status.fail(`ERROR caught while trying to play audio: ${e}`); |
| 104 | + next(); |
| 105 | + } |
| 106 | + }).catch((error) => { |
| 107 | + status.fail(`ERROR playing audio: ${error}`); |
| 108 | + next(); |
85 | 109 | }); |
86 | | - }).catch((error) => { |
87 | | - status.fail(`ERROR fetching audio file: ${error}`); |
88 | | - next(); |
89 | | - }); |
90 | 110 | } |
91 | 111 |
|
92 | 112 | interrupt() { |
|
109 | 129 | } |
110 | 130 |
|
111 | 131 | setPath(path) { |
112 | | - this.mAudioBlobPromise = fetch(path); |
| 132 | + this.mAudioPath = path; |
113 | 133 | } |
114 | 134 |
|
115 | 135 | execute(next, status) { |
116 | | - this.mAudioBlobPromise.then((response) => response.blob()) |
117 | | - .then((blob) => { |
118 | | - var audioURL = window.URL.createObjectURL(blob); |
119 | | - this.mAudio = new Audio(audioURL); |
120 | | - this.mAudio.addEventListener("ended", (event) => { |
121 | | - // note that this happens after we send back the response that we're done |
122 | | - // since this is supposed to stack and run in parallel |
123 | | - // as such, there can be NO status modifications here |
124 | | - console.log(`ended, time ${this.mTotalTime} length ${this.mTotalLength}`); |
125 | | - if (this.mTotalTime > this.mTotalLength) |
126 | | - return; |
127 | | - |
128 | | - var time = Math.random() * this.mIntervalLength; |
129 | | - this.mTotalTime += this.mMinInterval + time; |
130 | | - setTimeout(() => { |
131 | | - console.log("replay"); |
132 | | - this.mAudio.currentTime = 0; |
133 | | - this.mAudio.play(); |
134 | | - }, (this.mMinInterval + time) * 1000); |
135 | | - }); |
136 | | - this.mAudio.addEventListener("canplaythrough", (event) => { |
137 | | - this.mAudio.play() |
138 | | - .then(() => next()) |
139 | | - .catch((reason) => { |
140 | | - status.fail(`ERROR playing audio: ${reason}`); |
141 | | - next(); |
| 136 | + fetch(this.mAudioPath) |
| 137 | + .catch((error) => { |
| 138 | + status.fail(`ERROR fetching audio file: ${error}`); |
| 139 | + next(); |
| 140 | + }) |
| 141 | + .then((response) => { |
| 142 | + if (response.ok) { |
| 143 | + return response.blob(); |
| 144 | + } else { |
| 145 | + throw new Error(`Bad response from fetching audio file: ${response.status}`); |
| 146 | + } |
| 147 | + }) |
| 148 | + .catch((error) => { |
| 149 | + status.fail(`ERROR getting response blob: ${error}`); |
| 150 | + next(); |
| 151 | + }) |
| 152 | + .then((blob) => { |
| 153 | + try { |
| 154 | + var audioURL = window.URL.createObjectURL(blob); |
| 155 | + this.mAudio = new Audio(audioURL); |
| 156 | + this.mAudio.addEventListener("ended", (event) => { |
| 157 | + // note that this happens after we send back the response that we're done |
| 158 | + // since this is supposed to stack and run in parallel |
| 159 | + // as such, there can be NO status modifications here |
| 160 | + console.log(`ended, time ${this.mTotalTime} length ${this.mTotalLength}`); |
| 161 | + if (this.mTotalTime > this.mTotalLength) |
| 162 | + return; |
| 163 | + |
| 164 | + var time = Math.random() * this.mIntervalLength; |
| 165 | + this.mTotalTime += this.mMinInterval + time; |
| 166 | + setTimeout(() => { |
| 167 | + console.log("replay"); |
| 168 | + this.mAudio.currentTime = 0; |
| 169 | + this.mAudio.play(); |
| 170 | + }, (this.mMinInterval + time) * 1000); |
| 171 | + }); |
| 172 | + this.mAudio.addEventListener("canplaythrough", (event) => { |
| 173 | + this.mAudio.play() |
| 174 | + .catch((reason) => { |
| 175 | + status.fail(`ERROR playing audio: ${reason}`); |
| 176 | + next(); |
| 177 | + }) |
| 178 | + .then(() => next()); |
142 | 179 | }); |
| 180 | + } catch (e) { |
| 181 | + status.fail(`ERROR caught while trying to play audio: ${e}`); |
| 182 | + next(); |
| 183 | + } |
| 184 | + }) |
| 185 | + .catch((error) => { |
| 186 | + status.fail(`ERROR playing audio: ${error}`); |
| 187 | + next(); |
143 | 188 | }); |
144 | | - }).catch((error) => { |
145 | | - status.fail(`ERROR fetching audio file: ${error}`); |
146 | | - next(); |
147 | | - }); |
148 | 189 | } |
149 | 190 |
|
150 | 191 | interrupt() { |
|
0 commit comments