Skip to content

Commit

Permalink
fix: audio auto play when appended new audio if enable autoplayInitLo…
Browse files Browse the repository at this point in the history
…adPlayList #146
  • Loading branch information
lijinke666 committed Aug 30, 2020
1 parent 3f08c70 commit c3c40b3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
33 changes: 32 additions & 1 deletion __tests__/tests/instance.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { mount } from 'enzyme'
import React from 'react'
import ReactJkMusicPlayer from '../../src'

const getApp = (props) => {
Expand Down Expand Up @@ -176,4 +176,35 @@ describe('AudioInstance test', () => {
'b',
])
})

it('should set audio info when append audio list', () => {
const { instance, wrapper } = getApp()
const audioInfo = {
name: 'a',
singer: 'a',
musicSrc: 'c',
}
instance.appendAudio(0, [audioInfo])
expect(wrapper.state().name).toEqual(audioInfo.name)
expect(wrapper.state().musicSrc).toEqual(audioInfo.musicSrc)
expect(wrapper.state().singer).toEqual(audioInfo.singer)
})

it('should auto load if autoplayInitLoadPlayList is true when append audio list', () => {
const onAudioLoad = jest.fn()
window.HTMLMediaElement.prototype.load = () => {
onAudioLoad()
}
const { instance } = getApp({
autoplayInitLoadPlayList: true,
onAudioLoad,
})
const audioInfo = {
name: 'a',
singer: 'a',
musicSrc: 'c',
}
instance.appendAudio(0, [audioInfo])
expect(onAudioLoad).toHaveBeenCalled()
})
})
9 changes: 1 addition & 8 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,14 +577,7 @@ class Demo extends React.PureComponent {
<button
type="button"
onClick={() => {
this.audio.appendAudio(0, [
{
name: 'name',
singer: 'signer',
musicSrc:
'http://podcasts.protocol-radio.com/podcast/protocol-radio-331.m4a',
},
])
this.audio.appendAudio(0, audioList2)
}}
>
append audio
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ export default class ReactJkMusicPlayer extends PureComponent {
}
}

canPlay = () => {
onAudioCanPlay = () => {
this.setState({
loading: false,
playing: false,
Expand Down Expand Up @@ -1543,7 +1543,7 @@ export default class ReactJkMusicPlayer extends PureComponent {
target = this.audio,
eventsNames = {
waiting: this.loadAndPlayAudio,
canplay: this.canPlay,
canplay: this.onAudioCanPlay,
error: this.onAudioError,
ended: this.onAudioEnd,
pause: this.onAudioPause,
Expand Down Expand Up @@ -1863,6 +1863,7 @@ export default class ReactJkMusicPlayer extends PureComponent {
})
newAudioLists.splice(fromIndex, 0, ...addedAudioLists)
this.changeAudioLists({ ...this.props, audioLists: newAudioLists })
this.onAudioCanPlay()
}

getEnhanceAudio = () => {
Expand Down

0 comments on commit c3c40b3

Please sign in to comment.