Skip to content

Instantly share code, notes, and snippets.

@tajuszk
Last active November 21, 2020 15:26
Show Gist options
  • Save tajuszk/4d7fd28002a7a579310d0e76f42d1d13 to your computer and use it in GitHub Desktop.
Save tajuszk/4d7fd28002a7a579310d0e76f42d1d13 to your computer and use it in GitHub Desktop.
// 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