Skip to content

Commit

Permalink
fix: do not fade on iOS devices
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyCA committed Jan 14, 2021
1 parent 29c7a8d commit 767df65
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
51 changes: 51 additions & 0 deletions __tests__/tests/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1749,4 +1749,55 @@ describe('<ReactJkMusicPlayer/>', () => {
expect(onAudioPlay).not.toHaveBeenCalled()
expect(onAudioSeeked).toHaveBeenCalledTimes(1)
})

it('should never fade on iOS', async () => {
const platformGetter = jest.spyOn(window.navigator, 'platform', 'get')
platformGetter.mockReturnValue('iPhone')

const audio = {
volume: 0,
}
const fn = jest.fn()
const { fadeInterval, updateIntervalEndVolume } = adjustVolume(
audio,
audio.volume,
1,
{ duration: 200 },
fn,
)

expect(fadeInterval).toBeUndefined()
expect(updateIntervalEndVolume).toBeUndefined()
expect(fn).toHaveBeenCalledTimes(1)
expect(audio.volume).toStrictEqual(1)

platformGetter.mockRestore()
})

it('should respect fade on non-iOS', async () => {
const platformGetter = jest.spyOn(window.navigator, 'platform', 'get')
platformGetter.mockReturnValue('MacIntel')

const audio = {
volume: 0,
}
const fn = jest.fn()
const { fadeInterval, updateIntervalEndVolume } = adjustVolume(
audio,
audio.volume,
1,
{ duration: 200 },
fn,
)

expect(fadeInterval).not.toBeUndefined()
expect(updateIntervalEndVolume).not.toBeUndefined()

await sleep(500)

expect(fn).toHaveBeenCalledTimes(1)
expect(audio.volume).toStrictEqual(1)

platformGetter.mockRestore()
})
})
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ export default class ReactJkMusicPlayer extends PureComponent {
isAutoPlayWhenUserClicked: true,
},
() => {
if (fadeIn) {
if (fadeInInterval) {
this.audio.volume = startVolume
}
this.loadAndPlayAudio()
Expand Down
19 changes: 18 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ export const isSafari = () => {
)
}

// https://stackoverflow.com/a/9039885/2789451
export function isIOS() {
return (
[
'iPad Simulator',
'iPhone Simulator',
'iPod Simulator',
'iPad',
'iPhone',
'iPod',
].includes(navigator.platform) ||
// iPad on iOS 13 detection
(navigator.userAgent.includes('Mac') && 'ontouchend' in document)
)
}

// https://stackoverflow.com/questions/7451508/html5-audio-playback-with-fade-in-and-fade-out
export function swing(p) {
return 0.5 - Math.cos(p * Math.PI) / 2
Expand All @@ -60,7 +76,8 @@ export function adjustVolume(
callback,
) {
let delta = endVolume - startVolume
if (!delta || !duration || !easing || !interval) {

if (!delta || !duration || !easing || !interval || isIOS()) {
element.volume = endVolume
callback()
return { fadeInterval: undefined, updateIntervalEndVolume: undefined }
Expand Down

0 comments on commit 767df65

Please sign in to comment.