第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將音頻 isPlaying 狀態(tài)設(shè)置為對當(dāng)前音頻唯一

將音頻 isPlaying 狀態(tài)設(shè)置為對當(dāng)前音頻唯一

德瑪西亞99 2023-04-27 15:07:47
我正在獲取一堆曲目,每首曲目都有自己的按鈕來播放每首曲目。我可以點擊它們播放,然后再次點擊暫停音頻,但如果我嘗試播放新音頻而不暫停當(dāng)前正在播放的音頻,除非我雙擊它,否則它不會播放,因為 isPlaying 狀態(tài)將設(shè)置為 false(這是我切換播放/暫停的方式)。我希望能夠在 isPlaying 為 true 時單擊新音頻,而不是將該狀態(tài)更改為 false(但如果我單擊同一個按鈕兩次仍然能夠暫停當(dāng)前音頻)。謝謝!const App = () => {  const [tracks, setTracks] = React.useState(null);  const [currentSong, setCurrentSong] = useState(null);  const [currentId, setCurrentId] = useState(null);  const [isPlaying, setIsPlaying] = useState(false);  const audio = useRef();  // toggle function for toggling whether audio is played or paused  const togglePlay = () => {    setIsPlaying(!isPlaying);  };  // onClick function for handling currentSong and isPlaying state  const handleClickPlay = (track) => {    setCurrentSong(track);    setCurretId(track.id);    setIsPlaying(true);    if (isPlaying) {      togglePlay();    }  };  React.useEffect(() => {    if (currentSong) {      if (isPlaying) {        audio.current.play();      } else {        audio.current.pause();      }    }  }, [currentSong, isPlaying]);  React.useEffect(() => {    if (isPlaying) {      if (setCurrentSong) {        setIsPlaying(true);      }    }  }, []);  React.useEffect(() => {    fetch("/album.json")      .then((response) => response.json())      .then((data) => {        const tracks = Object.values(data.entities.tracks);        setTracks(tracks);      });  }, []);  if (!tracks) {    return <div>Loading...</div>;  }  console.log(currentSong);  console.log(currentId);  console.log(isPlaying);  return (    <div>      {currentSong && (        <audio src={currentSong.stems.full.lqMp3Url} ref={audio}></audio>      )}      <table>        <tbody>          {tracks.map((track) => (            <TrackRow              track={track}              handleClickPlay={handleClickPlay}              isPlaying={isPlaying}              id={currentId}              currentSong={currentSong}              trackMatchId={() => currentSong === currentId}            />          ))}        </tbody>      </table>    </div>  );};
查看完整描述

1 回答

?
哈士奇WWW

TA貢獻1799條經(jīng)驗 獲得超6個贊

你可以試試這個:


const handleClickPlay = (track) => {

  setCurrentSong(track);

  setCurrentId(track.id);

  if (track.id === currentId) togglePlay();

  else setIsPlaying(true);

};


查看完整回答
反對 回復(fù) 2023-04-27
  • 1 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號