Write a music player using html5

When creating a simple music player in HTML5, you can do so using the `<audio>` element. Here's a basic example:

<!DOCTYPE html>
<html>
<head>
    <title>音乐播放器</title>
</head>
<body>
    <h1>音乐播放器</h1>
    
    <audio controls>
        <source src="your_music.mp3" type="audio/mpeg">
        Your browser does not support the audio element.
    </audio>
</body>
</html>

In the example above, <audio>the element is used to embed an audio file, and controlsthe properties display the player's controls such as play, pause, and volume control. <source>Element is used to specify the source and type of the audio file.

You need to "your_music.mp3"replace with the path of the music file you want to play.

If you want to add more features, such as custom styles, playlists, autoplay, etc., you may need to use JavaScript to manipulate audio elements. Here's a slightly more advanced example:

<!DOCTYPE html>
<html>
<head>
    <title>音乐播放器</title>
    <style>
        /* 自定义样式 */
        #player {
            width: 300px;
            margin: 20px auto;
        }
    </style>
</head>
<body>
    <h1>音乐播放器</h1>
    
    <div id="player">
        <audio id="audioPlayer" controls>
            <source src="your_music.mp3" type="audio/mpeg">
            Your browser does not support the audio element.
        </audio>
        <button id="playButton">播放</button>
        <button id="pauseButton">暂停</button>
    </div>
    
    <script>
        const audioPlayer = document.getElementById('audioPlayer');
        const playButton = document.getElementById('playButton');
        const pauseButton = document.getElementById('pauseButton');
        
        playButton.addEventListener('click', () => {
            audioPlayer.play();
        });
        
        pauseButton.addEventListener('click', () => {
            audioPlayer.pause();
        });
    </script>
</body>
</html>

In the above example, we used a custom style to layout the player, added custom play and pause buttons, and implemented the play and pause functions through JavaScript code.

This is just a simple example, you can further customize and extend the player's functionality according to your needs. If you wish to implement a more complex music player, you may need to use some ready-made audio player libraries or frameworks.

Guess you like

Origin blog.csdn.net/canduecho/article/details/132526252