【问题标题】:How to do play next music and pause current music in dynamic content using javascript?如何使用javascript在动态内容中播放下一首音乐和暂停当前音乐?
【发布时间】:2020-08-07 10:09:45
【问题描述】:

您好,javascript 中有没有办法播放下一首歌曲并暂停当前正在播放的歌曲,因为这是动态的,并且它们具有相同的 id 和 onClick 值。我们如何在 javascript 中处理动态内容?

index.ejs

<% music.forEach(function(muzic){ %>
<div class="col-lg-3 col-md-6 col-sm-6">
    <div class="card">
        <img class="featured-image" src="/uploads/featured-img/<%= muzic.featured_img %>" alt="">
        <div class="card-img-overlay" id="toggleIcon">
            <a onClick="togglePlay()" class="select-play">
                <img class="select-play-btn" src="/img/select-play.png" id="play-pause" width="80">
                <audio id="myAudio" src="/uploads/audio-file/<%= muzic.audio %>"></audio>
            </a>
        </div>
    </div>
</div>
<% }); %>

脚本

<script>
var myAudio = document.getElementById("myAudio");
var isPlaying = false;

function togglePlay() {
    isPlaying ? myAudio.pause() : myAudio.play();

    var img = document.getElementById('play-pause').src;
    if (img.indexOf('select-play.png')!=-1) {
        document.getElementById('play-pause').src  = '/img/pause-outline.png';
    }
    else {
    document.getElementById('play-pause').src = '/img/select-play.png';
    }

    var element = document.getElementById("toggleIcon");
    element.classList.toggle("pause-style");
};

myAudio.onplaying = function() {
    isPlaying = true;
};
myAudio.onpause = function() {
    isPlaying = false;
};
</script>

【问题讨论】:

    标签: javascript node.js ejs


    【解决方案1】:

    您需要先获取音乐数组中的所有音乐 然后开始控制所有这些

    示例:

       // array who has all musics as playlist
        var Musics = [] ;
        // for target current played music in playlist Musics 
        var currentMusicPlayed = 0;
        
        // 3 paths of music
        let arrayOfPaths = ["./Music1.mp3" , "./Music2.mp3" , "./Music3.mp3"];
        
        // loop for making audio objects and push all of them in Musics Playlist array
        for(let i = 0 ; i < arrayOfPaths.length ; i += 1){
        
            let music = new Audio();
                music.src = arrayOfPaths[i];
        
            Musics.push(music);
        
        }
        
        // music player
        function MusicListPlayer(){
            // play current music who has 
            Musics[currentMusicPlayed].play();
        
            // and stop any music
            for(let i = 0 ; i < Musics.length ; i += 1){
                if(i != currentMusicPlayed){
                    Musics[currentMusicPlayed].pause();
                }
            }
        
            // if current music index >= must be start from 0 again
            if(currentMusicPlayed >= i ) currentMusicPlayed = 0;
            // else increment for next click 
            else currentMusicPlayed += 1;
        }
        
        // just a random element for example
        const buttonGetNextMusic = document.querySelector("any");
        
        // here in click MusicListPlayer must be called and make next music in Array work
        buttonGetNextMusic.addEventListener("click" , MusicListPlayer);
    

    我希望这个例子能帮助你理解这个想法

    【讨论】:

    • 让 arrayOfPaths = ["./Music1.mp3" , "./Music2.mp3" , "./Music3.mp3"];那怎么办?我如何从数据库中获取我的音频列表并分配一个这样的变量
    【解决方案2】:

    您可以在 togglePlay(1) 中传递值,并在代码中添加 if else 条件语句。

    function togglePlay(n){
        if(n==1){
           //do this
               }
       else   {
           //do that
              }
    }
    

    在 index.ejs 中通过 forEach 之类的函数再传递一个参数index

    <% music.forEach(function(muzic,index){ %>
    <div class="col-lg-3 col-md-6 col-sm-6">
        <div class="card">
            <img class="featured-image" src="/uploads/featured-img/<%= muzic.featured_img %>" alt="">
            <div class="card-img-overlay" id="toggleIcon">
                <a onClick="togglePlay(<%= index %>)" class="select-play">
                    <img class="select-play-btn" src="/img/select-play.png" id="play-pause" width="80">
                    <audio id="myAudio" src="/uploads/audio-file/<%= muzic.audio %>"></audio>
                </a>
            </div>
        </div>
    </div>
    <% }); %>
    

    使用 id 作为参数来识别音频并执行算法

    【讨论】:

    • 但它是动态的,我无法在每个 togglePlay() 中填充值
    • 我会推荐你​​使用 'music' 中的另一个对象作为 'playing' ,将值设为布尔值并通过 togglePlay() 传递它。
    • 嗨有一个想法,我将如何在 togglePlay() 参数中添加表 ID,如 onClick="togglePlay()" 但这是错误的。有没有办法将表 id 放入参数中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    相关资源
    最近更新 更多