【发布时间】:2019-07-19 10:47:53
【问题描述】:
我在 react 中创建了一个播放暂停按钮,但我只能管理一首歌曲。我想列出更多歌曲并想播放和暂停。我想一次播放一首歌曲。
我已经尝试过使用音频。
这是我的渲染函数 -
render() {
return (
<div>
<button getsrc="http://streaming.tdiradio.com:8000/house.mp3" onClick={this.togglePlay}>{this.state.play ? 'Pause' : 'Play'}</button><br /><br />
<button getsrc="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" onClick={this.togglePlay}>{this.state.play ? 'Pause' : 'Play'}</button>
<button getsrc="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3" onClick={this.togglePlay}>{this.state.play ? 'Pause' : 'Play'}</button>
<button getsrc="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3" onClick={this.togglePlay}>{this.state.play ? 'Pause' : 'Play'}</button>
</div>
);
}
这是我的构造函数和音频函数 -
constructor(props) {
super(props);
this.state = {
play: false
}
}
audio = new Audio("http://streaming.tdiradio.com:8000/house.mp3")
togglePlay = () => {
this.setState({ play: !this.state.play }, () => {
this.state.play ? this.audio.play() : this.audio.pause();
});
}
如何一次播放一首歌曲。并显示播放的歌曲信息
【问题讨论】:
标签: reactjs audio audio-player