Skip to content

Commit

Permalink
fix: cannot reset play index when audioLists changed #286
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Jan 27, 2021
1 parent 43b9dd5 commit e42177b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ ReactDOM.render(
| getAudioInstance | `(instance: HTMLAudioElement) => void` | `-` | get origin audio element instance , you can use it do everything |
| autoHiddenCover | `boolean` | `false` | Auto hide the cover photo if no cover photo is available |
| onBeforeAudioDownload | `(audioInfo: ReactJkMusicPlayerAudioInfo) => Promise<TransformedDownloadAudioInfo>` | `-` | transform download audio info before |
| clearPriorAudioLists | `boolean` | `false` | Replace a new playlist with the first loaded playlist, |
| clearPriorAudioLists | `boolean` | `false` | Replace a new playlist with the first loaded playlist and reset playIndex to 0 |
| autoPlayInitLoadPlayList | `boolean` | `false` | Play your new play list right after your new play list is loaded turn false. |
| spaceBar | `boolean` | `false` | Play and pause audio through space bar (Desktop effective). |
| showDestroy | `boolean` | `false` | Destroy player button display |
Expand Down
22 changes: 22 additions & 0 deletions __tests__/tests/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1800,4 +1800,26 @@ describe('<ReactJkMusicPlayer/>', () => {

platformGetter.mockRestore()
})

it('should reset play index to default value when audio lists changed', async () => {
const audioLists = [{ musicSrc: 'y' }]
const onPlayIndexChange = jest.fn()
const wrapper = mount(
<ReactJkMusicPlayer
audioLists={[{ musicSrc: 'x' }]}
autoPlay={false}
defaultPlayIndex={1}
clearPriorAudioLists
onPlayIndexChange={onPlayIndexChange}
/>,
)

wrapper.setProps({ audioLists })

await sleep(200)

expect(wrapper.state('playIndex')).toEqual(0)

wrapper.setProps({ audioLists })
})
})
2 changes: 1 addition & 1 deletion example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const webpack = require('webpack')

const HOST = '0.0.0.0'
const HOST = 'localhost'
const PORT = 8081

const getPublicPath = () => {
Expand Down
3 changes: 3 additions & 0 deletions src/config/player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const DEFAULT_PLAY_INDEX = 0
export const DEFAULT_VOLUME = 1
export const DEFAULT_REMOVE_ID = -1
29 changes: 22 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ import PROP_TYPES from './config/propTypes'
import { sliderBaseOptions } from './config/slider'
import { THEME } from './config/theme'
import { VOLUME_FADE } from './config/volumeFade'
import {
DEFAULT_PLAY_INDEX,
DEFAULT_VOLUME,
DEFAULT_REMOVE_ID,
} from './config/player'
import LOCALE_CONFIG from './locale'
import Lyric from './lyric'
import {
Expand Down Expand Up @@ -105,7 +110,7 @@ export default class ReactJkMusicPlayer extends PureComponent {
toggle: this.props.mode === MODE.FULL,
playing: false,
currentTime: 0,
soundValue: 100,
soundValue: DEFAULT_VOLUME * 100,
moveX: 0,
moveY: 0,
loading: false,
Expand All @@ -118,11 +123,12 @@ export default class ReactJkMusicPlayer extends PureComponent {
isInitAutoPlay: this.props.autoPlay,
isInitRemember: false,
loadedProgress: 0,
removeId: -1,
removeId: DEFAULT_REMOVE_ID,
isNeedMobileHack: IS_MOBILE,
audioLyricVisible: false,
isAutoPlayWhenUserClicked: false,
playIndex: this.props.playIndex || this.props.defaultPlayIndex || 0,
playIndex:
this.props.playIndex || this.props.defaultPlayIndex || DEFAULT_PLAY_INDEX,
canPlay: false,
currentVolumeFade: VOLUME_FADE.NONE,
currentVolumeFadeInterval: undefined,
Expand Down Expand Up @@ -702,7 +708,10 @@ export default class ReactJkMusicPlayer extends PureComponent {
playIndex = this.state.playIndex,
audioLists = this.state.audioLists,
) => {
return Math.max(0, Math.min(audioLists.length - 1, playIndex))
return Math.max(
DEFAULT_PLAY_INDEX,
Math.min(audioLists.length - 1, playIndex),
)
}

onCoverClick = (mode = MODE.FULL) => {
Expand Down Expand Up @@ -1150,6 +1159,7 @@ export default class ReactJkMusicPlayer extends PureComponent {
lyric: '',
currentLyric: '',
loadedProgress: 0,
playIndex: DEFAULT_PLAY_INDEX,
},
res,
)
Expand Down Expand Up @@ -2005,7 +2015,7 @@ export default class ReactJkMusicPlayer extends PureComponent {
? this.getLastPlayStatus()
: {
playMode: playMode || PLAY_MODE.order,
playIndex: playIndex || 0,
playIndex: playIndex || DEFAULT_PLAY_INDEX,
}

if (theme !== THEME.AUTO) {
Expand Down Expand Up @@ -2322,7 +2332,8 @@ export default class ReactJkMusicPlayer extends PureComponent {
playMode,
clearPriorAudioLists,
} = nextProps
if (!arrayEqual(audioLists)(this.props.audioLists)) {
const isEqualAudioLists = arrayEqual(audioLists)(this.props.audioLists)
if (!isEqualAudioLists) {
if (clearPriorAudioLists) {
this.changeAudioLists(nextProps)
} else {
Expand All @@ -2332,7 +2343,11 @@ export default class ReactJkMusicPlayer extends PureComponent {
this.initPlayer(audioLists, false)
}
}
this.updatePlayIndex(playIndex)
this.updatePlayIndex(
!isEqualAudioLists && clearPriorAudioLists
? DEFAULT_PLAY_INDEX
: playIndex,
)
this.updateTheme(theme)
this.updateMode(mode)
this.updatePlayMode(playMode)
Expand Down

0 comments on commit e42177b

Please sign in to comment.