简单的 HTML5 播放器,只有播放和音量

问题描述 投票:0回答:1

关于如何制作仅具有播放和音量的简单 HTML5 播放器有什么想法吗?这将控制我不希望任何人能够倒带的流。仅从流现在所在的位置停止/开始播放,而不是上次停止的位置。这个概念与广播电台相同。有什么想法吗?

谢谢

我已经 google 了 5 天了。

html streaming embed html5-audio htmlcontrols
1个回答
0
投票
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Audio Player</title>
    <style>
        .audio-player {
            display: flex;
            align-items: center;
            justify-content: space-around;
            width: 300px;
            margin: 50px auto;
            padding: 10px;
            border: 2px solid #000;
            border-radius: 10px;
            background-color: #f0f0f0;
        }
        .play-btn, .volume-control {
            cursor: pointer;
            font-size: 20px;
        }
        .volume-control input[type="range"] {
            width: 100px;
        }
        .play-btn {
            padding: 5px 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
        }
        .play-btn.pause {
            background-color: #FF5722;
        }
    </style>
</head>
<body>

<div class="audio-player">
    <button class="play-btn">Play</button>
    <div class="volume-control">
        <label for="volume">Volume:</label>
        <input type="range" id="volume" min="0" max="1" step="0.1" value="0.5">
    </div>
</div>

<audio id="audio-stream">
    <source src="your-stream-url-here" type="audio/mp3">
    Your browser does not support the audio element.
</audio>

<script>
    const playButton = document.querySelector('.play-btn');
    const audio = document.getElementById('audio-stream');
    const volumeControl = document.getElementById('volume');

    audio.addEventListener('timeupdate', function() {
        if (audio.currentTime > audio.buffered.end(0)) {
            audio.currentTime = audio.buffered.end(0);
        }
    });

    playButton.addEventListener('click', function() {
        if (audio.paused) {
            audio.play();
            playButton.textContent = 'Pause';
            playButton.classList.add('pause');
        } else {
            audio.pause();
            playButton.textContent = 'Play';
            playButton.classList.remove('pause');
        }
    });

    volumeControl.addEventListener('input', function() {
        audio.volume = volumeControl.value;
    });

    audio.addEventListener('loadedmetadata', function() {
        audio.currentTime = audio.duration;
    });
</script>

</body>
</html>

确保将

your-stream-url-here
替换为您的实际音频流 URL。

© www.soinside.com 2019 - 2024. All rights reserved.