Skip to content

Commit

Permalink
🔨 Add music player
Browse files Browse the repository at this point in the history
  • Loading branch information
shianqi committed Sep 28, 2017
1 parent fd2a490 commit 42c1c90
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/actions/musicPlayer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const MUSICPLAYER_PLAY = 'MUSICPLAYER_PLAY'
export const MUSICPLAYER_STOP = 'MUSICPLAYER_STOP'
export const MUSICPLAYER_PAUSE = 'MUSICPLAYER_PAUSE'

export const musicPlayer_play = ()=>({
type: MUSICPLAYER_PLAY
})

export const musicPlayer_pause = ()=>({
type: MUSICPLAYER_PAUSE
})
2 changes: 2 additions & 0 deletions src/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react'

import Footer from './Footer'
import Banner from './Banner/index'
import MusicPlayer from './MusicPlayer'
import MainWindow from './MainWindow'
import Login from './Login'

Expand All @@ -13,6 +14,7 @@ class App extends Component {
<div className={Style.app}>
<Footer/>
<MainWindow/>
<MusicPlayer/>
<Banner/>
<Login/>
</div>
Expand Down
18 changes: 17 additions & 1 deletion src/components/Footer/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import React, { Component } from 'react'
import Style from './index.css'
import cs from 'classnames'
import { connect } from 'react-redux'
import {
musicPlayer_play,
musicPlayer_pause
} from '../../actions/musicPlayer.jsx'

import {
iconfont,
icon_shangyiqu,
Expand All @@ -11,8 +17,17 @@ import {
icon_iconfontttpodicon1eps
} from 'iconfont.css'

const selector = (state)=>({
playing: state.musicPlayer.playing
})

class Footer extends Component {
render() {
const { dispatch } = this.props
const handlerPlay = ()=>{
dispatch(musicPlayer_play())
}

return(
<div className={Style.footer}>
<div>
Expand All @@ -22,6 +37,7 @@ class Footer extends Component {
/>
<div
className={ cs(iconfont, icon_bofang, Style['button_play']) }
onClick = { handlerPlay }
title="播放"
/>
<div
Expand Down Expand Up @@ -49,4 +65,4 @@ class Footer extends Component {
}
}

export default Footer
export default connect(selector)(Footer)
34 changes: 34 additions & 0 deletions src/components/MusicPlayer/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, { Component } from 'react'
import { withRouter } from 'react-router-dom'
import { connect } from 'react-redux'

const selector = (state) => {
return {
musicPlayer: state.musicPlayer
}
}

class MusicPlayer extends Component {
render() {
const { musicPlayer={} } = this.props
const audio = document.getElementById('musicPlayer')
const {
src='',
playing
} = musicPlayer

if(audio&&playing) {
audio.play()
}else if(audio) {
audio.pause()
}

return (
<div>
<audio id="musicPlayer" src={ src }></audio>
</div>
)
}
}

export default withRouter(connect(selector)(MusicPlayer))
4 changes: 3 additions & 1 deletion src/reducers/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import list from './userSongList'
import user from './login'
import songList from './songList'
import discoverMusic from './discoverMusic'
import musicPlayer from './musicPlayer'

const reducers = {
list,
user,
songList,
discoverMusic
discoverMusic,
musicPlayer
}

export default reducers
22 changes: 22 additions & 0 deletions src/reducers/musicPlayer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
MUSICPLAYER_PLAY
} from '../actions/musicPlayer.jsx'

const _state = {
playing: false,
src: 'http://m10.music.126.net/20170928213730/a921719863a66fec83329a5b7f7b4b55/ymusic/f1ae/0bd1/31a9/5d64960d0cbebc0d089bc85a6ef54680.mp3'
}

const musicPlayer = (state = _state, action)=> {
switch (action.type) {
case MUSICPLAYER_PLAY:
return {
...state,
playing: true
}
default:
return state
}
}

export default musicPlayer

0 comments on commit 42c1c90

Please sign in to comment.