一次播放一次音频

时间:2020-03-02 09:58:30

标签: javascript reactjs audio next.js

我正在为项目使用react-use-audio-player,并且一次只需要播放一个音频。我在同一页面中有多个音频。目前,所有音频在加载时都处于暂停状态,这正是我所需要的。但是,当播放音频时,我没有停顿地单击下一个音频,第一个音频彼此重叠。我在下面添加了我的代码供您参考。谢谢有人能帮助我。

这是我的pages/index.js

import React from 'react'
import { AudioPlayerProvider } from 'react-use-audio-player'
import AudioPlayer from '../../../components/AudioPlayer'

const Home = () => {

  return (
    <>
     <ul>
      <li>
            <div className='color_c uk-flex uk-flex-middle uk-flex-center uk-text-center'>
              <div>
                <AudioPlayerProvider>
                  <AudioPlayer file='../../../projectFolder/audio/c_chai-yi-ning_90s.mp3' playButton='/projectFolder/img/icon_play_c.svg' pauseButton='/projectFolder/img/icon_pause_c.svg' />
                </AudioPlayerProvider>
                <div className='uk-text-small'>Chai Yi Ning, 19</div>
              </div>
            </div>
          </li>
         <li>
            <div className='color_c uk-flex uk-flex-middle uk-flex-center uk-text-center'>
              <div>
                <AudioPlayerProvider>
                  <AudioPlayer file='../../../projectFolder/audio/c_chai-yi-ning_90s.mp3' playButton='/projectFolder/img/icon_play_c.svg' pauseButton='/projectFolder/img/icon_pause_c.svg' />
                </AudioPlayerProvider>
                <div className='uk-text-small'>Ying Ming, 54</div>
              </div>
            </div>
          </li>
         <li>
            <div className='color_c uk-flex uk-flex-middle uk-flex-center uk-text-center'>
              <div>
                <AudioPlayerProvider>
                  <AudioPlayer file='../../../projectFolder/audio/c_chai-yi-ning_90s.mp3' playButton='/projectFolder/img/icon_play_c.svg' pauseButton='/projectFolder/img/icon_pause_c.svg' />
                </AudioPlayerProvider>
                <div className='uk-text-small'>Lee Chong Wie, 29</div>
              </div>
            </div>
         </li>
     </ul>
    </>
  )
}

export default Home

这是我的components/AudioPlayer.js

import React from 'react'
import { useAudioPlayer } from 'react-use-audio-player'

const AudioPlayer = ({ file, playButton, pauseButton }) => {
  const { play, pause, ready, loading, playing } = useAudioPlayer({
    src: file,
    format: 'mp3',
    autoplay: false
  })

  const togglePlay = () => {
    if (playing) {
      pause()
    } else {
      play()
    }
  }

  if (!ready && !loading) return <div>No audio to play</div>
  if (loading) return <div>Loading audio</div>

  return (
    <>
      <img src={!playing ? playButton : pauseButton} onClick={togglePlay} className='uk-width-2-3' style={{ cursor: 'pointer' }} />
    </>
  )
}

export default AudioPlayer

1 个答案:

答案 0 :(得分:0)

添加 生命周期 方法将解决此问题,每次卸载组件 返回 ,因此,如果您要更改歌曲,则 return 块将检查音频是否正在播放,并暂停歌曲,然后播放下一首歌曲。

public getLot(id: string){
    var _this = this;

    var result = [];
    var ids = [];
    ids = id.split('-');
    ids.forEach(function(product_id){
        if (_this.products[product_id] == null) {
            _this.products[product_id] = new ReplaySubject<any>();
            _this.http.get(`https://xxxx.com/api/products/${product_id}`)
                .subscribe(data => {
                    _this.products[product_id].next(data)
                });
        }

        result.push(_this.products[product_id]);
    });

    return result;
}