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?

音楽ファイルの再生

Last updated at Posted at 2024-08-19

はじめに

初心者でもミュージックの再生、停止することは簡単だったので実装してみた。

機能

再生アイコンを押すと音楽が流れ始め表示がSTOPアイコンに切り替わるボタンの実装。

実装

HTML

<!DOCTYPE html>
<html lang="jp">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="content">
        <img src="images/play.png" id="icon" />
    </div>
    </div>

    <audio id="mySong">
        <source src="gameover.mp3" type="audio/mp3" />
    </audio>

    <script src="js/audio.js"></script>
</body>

</html>

Javascript

const mySong = document.querySelector('#mySong');
const icon = document.querySelector('#icon');

icon.onclick = function () {
    if (mySong.paused) {
        mySong.play();
        icon.src = "images/pause.png";
    } else {
        mySong.pause();
        icon.src = "images/play.png";
    }
};



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?