1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Webページ上でaudioを使う時はDOMノードに足さなくてもいい

1
Posted at

実際のDOMノードに<audio>タグを足さなくても、

const audio = new Audio();

という形でjavascript上でHTMLAudioElementを作成するだけで音声は再生できます。
HTMLAudioElementは<audio>をjavascriptから操作するためのメソッドを提供するためのインタフェースなので、<audio>タグに対し、jsで行うことの出来る操作は一通り実行できますし、イベントハンドラの設定も可能です。

当然、見た目には反映されない

<audio>タグを使うと基本的にブラウザ標準の音声インタフェースが出ますが、
js上でHTMLAudioElementを作成した場合はDOMツリー上に載らないので全く見た目には影響しません。
元より既存の見た目に頼るつもりがない場合は、こちらの方が取り回しが楽ではないかと個人的には思います。
見た目部分と切り離せますので。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?