Last active
November 21, 2020 15:26
-
-
Save tajuszk/4d7fd28002a7a579310d0e76f42d1d13 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// audio要素作成 | |
const audio = document.createElement('audio') | |
// 音声ファイルを登録 | |
const source = audio.appendChild(document.createElement('source')) | |
source.setAttribute('src', '../sounds/default.mp3') | |
audio.appendChild(source) | |
document.body.appendChild(audio) | |
// 起動時以外のタイミングでplayを実行しないとエラーが起きるので関数を分けておく | |
function play () { | |
// 音声再生 | |
audio.play() | |
.then(function (result) { | |
console.log('成功') | |
console.log(result) | |
}) | |
.catch(function (exception) { | |
console.error('エラーが発生しました') | |
console.error(exception) | |
}) | |
.finally(function (result) { | |
console.log('成功/失敗 共通処理') | |
console.log(result) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment