Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

в аудио плеере выключение композиции не работает, кнопки play stop дублируются,…

  1. User

    в аудио плеере выключение композиции не работает, кнопки play stop дублируются, выключение композиции не работает, прогресс-бар не работает:

    index:

    <title>Home</title>
    
    				<div class="form-group">
    

    Search by genre: All

    function setProgress(index, value) { const audio = document.getElementById(audio - $ { index }); audio.currentTime = (value / 100) * audio.duration; }

    function updateProgress(index) { const audio = document.getElementById(audio - index);constprogressBar=document.getElementById(‘progress−bar− { index }); const progressBar = document.getElementById(`progress-bar-{index}); const progressTime = document.getElementById(progress-time-$ {index}`); const value = (audio.currentTime / audio.duration) * 100; progressBar.value = value; const formattedTime = formatTime(audio.currentTime); progressTime.innerHTML = formattedTime; }

    function formatTime(time) { const minutes = Math.floor(time / 60); const seconds = Math.floor(time % 60); const formattedSeconds = seconds < 10 ? 0 seconds:seconds;return{ seconds } : seconds; return { minutes }: $ { formattedSeconds };

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    
    <script>
    

    const playButtons = document.querySelectorAll('.fa-play'); playButtons.forEach((button, index) => { const audio = document.querySelector(#audio-${index}); button.addEventListener('click', () => { audio.play(); }); });

    <script>
    $("#city").autocomplete({
    source: '/autocomplete/cities',
    minLength: 1,
    

    });

    const queryInput = document.querySelector("#query"); const roleInput = document.querySelector("#role"); const cityInput = document.querySelector("#city");

    queryInput.value = "<%= query %>"; roleInput.value = "<%= role %>"; cityInput.value = cityInput.getAttribute('data-value');

    const query = queryInput.value; const role = roleInput.value; const city = cityInput.value; const genres = ["Rock", "Pop", "Jazz", "Blues", "Country"]; const genreSelect = document.querySelector("#genre"); genres.forEach((genre) => { const option = document.createElement("option"); option.value = genre; option.text = genre; genreSelect.appendChild(option); });

    css:

    /* audio player */ .audio-player { margin: 50px auto; max-width: 800px; }

    .audio-player-inner { display: flex; flex-direction: column; justify-content: center; align-items: center; overflow-x: auto; -webkit-overflow-scrolling: touch; }

    .audio-tracklist { display: flex; flex-direction: row; justify-content: center; align-items: center; flex-wrap: wrap; margin: 0; padding: 0; list-style: none; }

    .audio-track { display: flex; flex-direction: column; justify-content: center; align-items: center; margin: 20px; max-width: 300px; text-align: center; height: auto; position: relative; transition: transform 0.3s; overflow: hidden; }

    .audio-track:hover { transform: scale(1.05); }

    .audio-track-image { width: 100%; height: 0; padding-bottom: 100%; position: relative; }

    .audio-track-image img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; filter: brightness(100%); transition: filter 0.3s; }

    .audio-track:hover img { filter: brightness(70%); }

    .audio-track-image-overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; opacity: 0; transition: opacity 0.3s; background: rgba(0, 0, 0, 0.5); }

    .audio-track:hover .audio-track-image-overlay { opacity: 1; }

    .audio-player-wrapper { display: flex; flex-direction: column; justify-content: center; align-items: center; opacity: 0; transition: opacity 0.3s; }

    .audio-track:hover .audio-player-wrapper { opacity: 1; }

    .audio-player-progress { display: flex; justify-content: center; align-items: center; width: 100%; margin-top: 10px; margin-bottom: 5px; }

    .progress-bar { width: 100%; height: 5px; -webkit-appearance: none; appearance:none; background: #ddd; outline: none; cursor: pointer; }

    .progress-bar::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 15px; height: 15px; border-radius: 50%; background: #333; cursor: pointer; }

    .progress-bar::-moz-range-thumb { width: 15px; height: 15px; border-radius: 50%; background: #333; cursor: pointer; }

    .progress-time { font-size: 12px; margin-left: 10px; color: #666; }

    .audio-player-controls { display: flex; justify-content: center; align-items: center; width: 100%; }

    .audio-player-button { display: flex; justify-content: center; align-items: center; border: none; background: none; cursor: pointer; margin: 0 10px; }

    .audio-player-icon { font-size: 24px; font-weight: 900; color: #fff; position: relative; z-index: 1; }

    .audio-player-button:hover .audio-player-icon { color: #1DB954; }

    .audio-player-button:focus { outline: none; }

    .audio-player-button:active { transform: translateY(1px); }

    @media screen and (max-width: 600px) { .audio-track { margin: 20px 0; } }

  2. Assistant

    Используя коды, которые вы предоставили, я выяснил, что есть проблема с функциями в JavaScript коде. Я изменил эти функции и теперь аудио плеер должен работать корректно.

    index:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta content="width=device-width, initial-scale=1.0" name="viewport">
        <meta content="ie=edge" http-equiv="X-UA-Compatible">
        <script src="/jquery/dist/jquery.min.js"></script>
        <script src="/jquery-ui/dist/jquery-ui.min.js"></script>
        <link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700" rel="stylesheet">
        <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
        <link href="/css/main.css" rel="stylesheet">
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
        <link href="/jquery-ui/themes/base/all.css" rel="stylesheet">
        <title>Home</title>
    </head>
    
    <body>
        <!-- ваш основной код -->
        <!-- audio player -->
        <div class="audio-player">
            <div class="audio-player-inner">
                <div class="audio-tracklist">
                    <% tracks.forEach((track, index) => { %>
                    <div class="audio-track">
                        <div class="audio-track-image">
                            <img src="/img/<%= track.image_filename || 'default-track-image.png' %>"
                                alt="<%= track.title %>">
                            <div class="audio-track-image-overlay">
                                <button class="audio-player-button" onclick="togglePlay(<%= index %>)">
                                    <i class="audio-player-icon fa fa-play"></i>
                                </button>
                            </div>
                        </div>
                        <div class="audio-track-info">
                            <h4><%= track.title %></h4>
                            <p class="album-title"><%= track.album_title || "No Album" %></p>
                        </div>
                        <audio id="audio-<%= index %>" src="/tracks/<%= track.filename %>"></audio>
                        <div class="audio-player-wrapper">
                            <div class="audio-player-progress">
                                <input type="range" id="progress-bar-<%= index %>" class="progress-bar" value="0"
                                    oninput="setProgress(<%= index %>, this.value)">
                                <span class="progress-time" id="progress-time-<%= index %>">0:00</span>
                            </div>
                            <div class="audio-player-controls">
                                <button class="audio-player-button" id="play-pause-<%= index %>"
                                    onclick="togglePlay(<%= index %>)">
                                    <i class="audio-player-icon fa fa-play"></i>
                                </button>
                                <button class="audio-player-button" id="stop-<%= index %>"
                                    onclick="stopAudio(<%= index %>)">
                                    <i class="audio-player-icon fa fa-stop"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                    <% }); %>
                </div>
            </div>
        </div>
        <!-- end -->
    
        <!-- Ваш основной код -->
        <script>
            function togglePlay(index) {
                const audio = document.getElementById(`audio-${index}`);
                if (audio.paused) {
                    audio.play();
                    setInputValue(index, true);
                    updateTime(index);
                } else {
                    audio.pause();
                    setInputValue(index, false);
                }
            }
    
            function setProgress(index, value) {
                const audio = document.getElementById(`audio-${index}`);
                audio.currentTime = (value / 100) * audio.duration;
                updateTime(index);
            }
    
            function stopAudio(index) {
                const audio = document.getElementById(`audio-${index}`);
                audio.pause();
                audio.currentTime = 0;
                setInputValue(index, false);
            }
    
            function updateTime(index) {
                const audio = document.getElementById(`audio-${index}`);
                const progressTime = document.getElementById(`progress-time-${index}`);
                const progressBar = document.getElementById(`progress-bar-${index}`);
    
                progressBar.value = (audio.currentTime / audio.duration) * 100;
                progressTime.innerHTML = formatTime(audio.currentTime);
    
                if (!audio.paused) {
                    setTimeout(() => {
                        updateTime(index);
                    }, 1000);
                }
            }
    
            function setInputValue(index, isPlaying) {
                const playPauseButton = document.getElementById(`play-pause-${index}`);
                const icon = playPauseButton.querySelector(".audio-player-icon");
    
                if (isPlaying) {
                    icon.classList.remove("fa-play");
                    icon.classList.add("fa-pause");
                } else {
                    icon.classList.remove("fa-pause");
                    icon.classList.add("fa-play");
                }
            }
    
            function formatTime(time) {
                const minutes = Math.floor(time / 60);
                const seconds = Math.floor(time % 60);
                const formattedSeconds = seconds < 10 ? `0${seconds}` : seconds;
                return `${minutes}:${formattedSeconds}`;
            }
        </script>
    </body>
    </html>
    

    CSS:

    /* Ваши основные стили... */
    

    В случае, если возникают ошибки, обратитесь к мне обратно.