简体   繁体   English

如何一次播放数组中的一首歌反应js

[英]How to play one song from array at a time react js

I am building a simple music player but where I fail is at trying to execute one item from the array at a time.我正在构建一个简单的音乐播放器,但我失败的地方是尝试一次执行数组中的一个项目。 I am using React H5 Audio Player package to play the music.我正在使用 React H5 音频播放器 package 来播放音乐。 I am currently mapping through all the songs but I don't know how to properly play one at a time.我目前正在映射所有歌曲,但我不知道如何正确地一次播放一首。 I appreciate any help.我感谢任何帮助。 I've been stuck on this for a few days.我已经坚持了几天了。


import { SongContext } from '../../SongContext';

import AudioPlayer from 'react-h5-audio-player';
import 'react-h5-audio-player/lib/styles.css';

import './Player.css';

const Player = () => {
    const { songs } = useContext(SongContext);

    return (
        <>
            {songs.length > 0 &&
                songs.map((song) => (
                    <div className="player" key={song.id}>
                        <AudioPlayer
                            // autoPlay
                            // src={song.preview}
                            showJumpControls={false}
                            customVolumeControls={[]}
                            customAdditionalControls={[]}
                            onPlay={() => console.log('playing')}
                        />
                    </div>
                ))}
        </>
    );
};
export default Player;

Don't map all the songs at once, take a song by index ( currentSong ), and when it's done, use the onEnded event to increment the index, so the next one would play.不要一次 map 所有歌曲,按索引 ( currentSong ) 取一首歌曲,完成后,使用onEnded事件增加索引,这样下一首就会播放。

Example ( codepen ):示例( codepen ):

const Player = () => {
  const { songs } = useContext(SongContext);
  const [currentSong, setCurrentSong] = useState(0);

  const song = songs[currentSong];
  
  if(!song) return null; // don't render the player when no song is available

  return (
    <div className="player">
      <AudioPlayer
        autoPlay
        src={song.preview}
        showJumpControls={false}
        customVolumeControls={[]}
        customAdditionalControls={[]}
        onPlay={() => console.log('playing')}
        onEnded={() => setCurrentSong(i => i + 1)}
      />
    </div>
  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一次只播放一首歌曲 (React) - Play only one song at a time (React) 如何使用SoundCloud API一次播放一首歌曲 - How to play a song one at the time using SoundCloud API JS React 如何查看歌曲的当前时间并对其进行倒计时? - How to check the current time of song and make the countdown of it JS React? 在播放列表中的特定时间播放歌曲 - Play song at specific time position from a playlist 如何按日期和时间播放最新歌曲 - How to play the most recent song by date and time Javascript:播放数组中的随机歌曲(列表) - Javascript: play a random song from an array (list) 如何在 ReactJS 和 AG Grid 中一次播放数组中的一个视频 - How to play one video from array at a time in ReactJS & AG Grid 一首歌在反应音频播放器中结束后,我想播放下一首歌曲。 我已经获得了指向数组的所有歌曲链接,但不确定如何实现 func - After a song ends in react audio player I want to play the next song. I've gotten all the song links to an array but not sure how to implement a func 如何在javascript中从随机时间播放歌曲? (例如:从第 5 秒开始) - How to play a song from a random time in javascript ? (Ex:from the 5th second) 让timeOut()每秒播放一首歌曲 - Getting timeOut() to play one song per second out of an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM