4.24.2 • Published 3 years ago

react-jinke-music-player v4.24.2

Weekly downloads
826
License
MIT
Repository
github
Last release
3 years ago

:package: Installation

using yarn :

yarn add react-jinke-music-player

using npm :

npm install react-jinke-music-player --save

:framed_picture: Screenshots

mini mode

mini mode

Light Theme

light theme

Dark Theme

dark theme

mobile

mobile

:sparkles: Feature list

:eyes: Example

live example :

local example : http://localhost:8081/

Source Code

:memo: Usage

import React from 'react'
import ReactDOM from 'react-dom'
import ReactJkMusicPlayer from 'react-jinke-music-player'
import 'react-jinke-music-player/assets/index.css'

ReactDOM.render(
  <ReactJkMusicPlayer {...options} />,
  document.getElementById('root'),
)

:clipboard: API

NameTypeDefaultDescription
classNamestring-Additional CSS class for the root DOM node
audioListsAudioListProps[]-Detail
themelight | dark | autodarkcolor of the music player theme dark, light, auto (follow system)light
localezh_CN | en_US | CustomLocaleen_USDetaillight
iconCustomize player icon-Customize player iconlight
defaultPositionobject{top:0,left:0}audio controller initial position with left,top,right,and bottom
playModeShowTimenumber600play mode toggle show time (ms)
boundsobject,numberbodyspecifies movement boundaries. Accepted values: parent restricts movement within the node's offsetParent (nearest node with position relative or absolute), or a selector, restricts movement within the targeted node An object with left, top, right, and bottom properties. These indicate how far in each direction the draggable can be moved.
preloadboolean,stringfalseWhether to load audio immediately after the page loads. can be set to auto | metadata | none true | falseifpreload=true preload="auto"
rememberbooleanfalseThe next time you access the player, do you keep the last state
glassBgbooleanfalseWhether the player's background displays frosted glass effect
removebooleantrueThe Audio Can be deleted
defaultPlayIndexnumber0Default play index of the audio player
playIndexnumber0play index of the audio player
defaultPlayModestringorderdefault play mode of the audio player options can be set to order,orderLoop,singleLoop,shufflePlay or omitted
modestringminiaudio theme switch checkedText can be set to mini,full or omitted
oncebooleanfalseThe default audioPlay handle function will be played again after each pause, If you only want to trigger it once, you can set 'true'
autoPlaybooleantrueWhether the audio is played after loading is completed. mobile devices are invalid autoplay-policy-changes
toggleModebooleantrueWhether you can switch between two modes, full => mini or mini => full
dragbooleantrueaudio controller is can be drag of the "mini" mode
seekedbooleantrueWhether you can drag or click the progress bar to play in the new progress.
showMiniModeCoverbooleantrueaudio cover is show of the "mini" mode
showMiniProcessBarbooleanfalseaudio progress circle bar is show of the "mini" mode
showProgressLoadBarbooleantrueDisplays the audio load progress bar.
showPlaybooleantrueplay button display of the audio player panel
showReloadbooleantruereload button display of the audio player panel
showDownloadbooleantruedownload button display of the audio player panel
showPlayModebooleantrueplay mode toggle button display of the audio player panel
showThemeSwitchbooleantruetheme toggle switch display of the audio player panel
showLyricbooleanfalseaudio lyric button display of the audio player panel
showMediaSessionbooleanfalsehttps://web.dev/media-session/
lyricClassNamestring-audio lyric class name
extendsContentReactNode \| boolean \| string-Extensible custom content like <><button>button1</button> <button>button2</button></>
defaultVolumenumber1default volume of the audio player , range 0-1
loadAudioErrorPlayNextbooleantrueWhether to try playing the next audio when the current audio playback fails
responsivebooleantrueWhether to turn on the response mode, if set false, audio controller always show desktop ui
onAudioDownloadfunction(audioInfo)-audio is downloaded handle
onAudioPlayfunction(audioInfo)-audio play handle
onAudioPausefunction(audioInfo)-audio pause handle
onAudioSeekedfunction(audioInfo)-When the user has moved/jumped to a new location in audio handle
onAudioVolumeChangefunction(volume)-When the volume has changed handle min = 0.0 max = 1.0
onAudioEndedfunction(currentPlayId,audioLists,audioInfo)-The single song is ended handle
onAudioAbortfunction(currentPlayId, audioLists, audioInfo)-audio load abort The target event like {...,audioName:xx,audioSrc:xx,playMode:xx}
onAudioProgressfunction(audioInfo)-audio play progress handle
onAudioErrorfunction(errMsg,currentPlayId, audioLists, audioInfo)-audio load failed error handle
onAudioReloadfunction(audioInfo)-audio reload handle
onAudioListsChangefunction(currentPlayId,audioLists,audioInfo)-audio lists change handle
onAudioPlayTrackChangefunction(currentPlayId,audioLists,audioInfo)-audio current play track change handle
onAudioPlayModeChangefunction(playMode)-play mode change handle
onAudioListsPanelChangefunction(panelVisible)-audio lists panel change handle
onThemeChangefunction(theme)-theme change handle
onModeChangefunction(mode)-mode change handle
onAudioListsSortEndfunction(oldIndex,newIndex)-audio lists sort end handle, use SortableJS
onAudioLyricChangefunction(lineNum, currentLyric)-audio lyric change handle
getContainer() => HTMLElement | Selectorsdocument.bodyReturn the mount node for Music player
getAudioInstance(instance: HTMLAudioElement) => void-get origin audio element instance , you can use it do everything
autoHiddenCoverbooleanfalseAuto hide the cover photo if no cover photo is available
onBeforeAudioDownload(audioInfo: ReactJkMusicPlayerAudioInfo) => Promise<TransformedDownloadAudioInfo>-transform download audio info before
clearPriorAudioListsbooleanfalseReplace a new playlist with the first loaded playlist and reset playIndex to 0
autoPlayInitLoadPlayListbooleanfalsePlay your new play list right after your new play list is loaded turn false.
spaceBarbooleanfalsePlay and pause audio through space bar (Desktop effective).
showDestroybooleanfalseDestroy player button display
onBeforeDestroyfunction(currentPlayId,audioLists,audioInfo)-custom destroy handler before
onDestroyedfunction(currentPlayId,audioLists,audioInfo)-player destroyed handle
customDownloaderfunction(downloadInfo: TransformedDownloadAudioInfo)-custom download handle
onCoverClickfunction(mode,audioLists,audioInfo)-audio cover clicked handle
onPlayIndexChangefunction(playIndex)-audio play index change handle
quietUpdatebooleanfalseDetail
renderAudioTitle(audioInfo, isMobile) => ReactNode-use locale.audioTitle to set audio tag title, the api can render custom jsx element for display
mobileMediaQuerystring(max-width: 768px) and (orientation : portrait)custom mobile media query string, eg use the mobile version UI on iPad. https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
volumeFade{ fadeIn: number(ms), fadeOut: number(ms) }-audio fade in and out. Detail
sortableOptionsobject{swap: true, animation: 100, swapClass: 'audio-lists-panel-sortable-highlight-bg'}SortableJs Options
restartCurrentOnPrevbooleanfalseRestarts the current track when trying to play previous song, if the current time of the song is more than 1 second

:bulb: Custom operation ui

Support feature:

  • play
  • pause
  • reload
  • change play time
  • change playback rate
  • change volume
  • destroy audio player
  • toggle play
  • clear audio list
  • toggle play
  • play next audio
  • play prev audio
  • play audio by custom play index
  • update play index
  • SortableJS methods
class App extends React.Component {
  constructor() {
    this.audioInstance = null
  }
  render() {
    return (
      <>
        <ReactJkMusicPlayer
          getAudioInstance={(instance) => {
            this.audioInstance = instance
          }}
        />
        <button onClick={() => this.audioInstance.play()}>play</button>
        <button onClick={() => this.audioInstance.pause()}>pause</button>
        <button onClick={() => this.audioInstance.load()}>reload</button>
        <button onClick={() => this.audioInstance.currentTime = 40}>
          change current play time
        </button>
        <button onClick={() => this.audioInstance.playbackRate = 2}>
          change play back rate
        </button>
        <button onClick={() => this.audioInstance.volume = 0.2}>
          change volume
        </button>
        <button onClick={() => this.audioInstance.destroy()}>
          destroy player
        </button>
        <button onClick={this.audio.togglePlay}>toggle play</button>
        <button onClick={this.audio.clear}>clear audio lists</button>
        <button onClick={this.audio.playNext}>play next</button>
        <button onClick={this.audio.playPrev}>play prev</button>
        <button onClick={() => this.audio.playByIndex(1)}>play by index</button>
        <button onClick={() => this.audio.updatePlayIndex(1)}>
          update play index
        </button>
      </>
    )
  }
}

:bulb: Glass bg

<ReactJkMusicPlayer glassBg />

:bulb: Custom downloader

Default use downloadjs, you can use any download library

eg. https://www.npmjs.com/package/file-saver, or use download attribute

const customDownloader = (downloadInfo) => {
  const link = document.createElement('a')
  link.href = downloadInfo.src // a.mp3
  link.download = downloadInfo.filename || 'test'
  document.body.appendChild(link)
  link.click()
}

;<ReactJkMusicPlayer
  audioLists={[{ src: 'a.mp3' }]}
  customDownloader={customDownloader}
/>

// use onBeforeAudioDownload
const onBeforeAudioDownload = () => {
  return Promise.resolve({
    src: '1.mp3',
  })
}

const customDownloader = (downloadInfo) => {
  console.log(downloadInfo.src) // 1.mp3
}

;<ReactJkMusicPlayer
  customDownloader={customDownloader}
  onBeforeAudioDownload={onBeforeAudioDownload}
/>

:bulb: Destroy player

const onBeforeDestroy = (currentPlayId, audioLists, audioInfo) => {
  return new Promise((resolve, reject) => {
    // your custom validate
    if (window.confirm('Are you confirm destroy the player?')) {
      // if resolve, player destroyed
      resolve()
    } else {
      // if reject, skip.
      reject()
    }
  })
}

const onDestroyed = (currentPlayId, audioLists, audioInfo) => {
  console.log('onDestroyed:', currentPlayId, audioLists, audioInfo)
}

;<ReactJkMusicPlayer
  showDestroy
  onBeforeDestroy={onBeforeDestroy}
  onDestroyed={onDestroyed}
/>

:bulb: Media session

https://web.dev/media-session/

// so cool, so easy !!!
<ReactJkMusicPlayer showMediaSession />

:bulb: Internationalization

Version: 4.11.0

Source

import Locale from 'react-jinke-music-player/lib/config/locale'
// Two languages are provided by default
// one of zh_CN | en_US
<ReactJkMusicPlayer locale={Locale.zh_CN}/>
// <ReactJkMusicPlayer locale={'en_US'}/>

// Custom override
const customLocale = {
  playModeText: {
    order: '',
    orderLoop: '',
    singleLoop: '',
    shufflePlay: ''
  },
  openText: '',
  closeText: '',
  emptyText: '',
  clickToPlayText: '',
  clickToPauseText: '',
  nextTrackText: '',
  previousTrackText: '',
  reloadText: '',
  volumeText: '',
  playListsText: '',
  toggleLyricText: '',
  toggleMiniModeText: '',
  destroyText: '',
  downloadText: '',
  lightThemeText: '',
  darkThemeText: '',
  switchThemeText: '',
  removeAudioListsText: '',
  controllerTitle: '',
  emptyLyricText: '',
  clickToDeleteText: (name) => ``,
  audioTitle: ''
  // audioTitle: (audioInfo) => ``
}

<ReactJkMusicPlayer locale={customLocale}/>

// Support partial override, auto merge

<ReactJkMusicPlayer locale={{ audioTitle: "xxx" }}/>

:bulb: Customize Theme

import ReactJkMusicPlayer from 'react-jinke-music-player'
import 'react-jinke-music-player/lib/styles/index.less'
@primary-color: #31c27c;
@default-color: #d9d9d9;
@bg-color: #f7f8fa;
@border-color: #d9d9d9;
@panel-bg-light: #fff;
@controller-bg-light: #fff;
@music-player-panel-height: 80px;
@lists-panel-width: 480px;
@lists-panel-height: 410px;
@lists-panel-item-bg: #40444b;
@lists-panel-item-bg-light: #fff;
@panel-header-height: 50px;
@panel-bg: rgba(0, 0, 0, 0.7);
@font-color: #444;
@player-width: 80px;
@player-height: @player-width;
@base-color: rgba(255, 255, 255, 0.6);
@common-animate-type: cubic-bezier(0.43, -0.1, 0.16, 1.1);
@common-animate-time: 350ms @common-animate-type;
@progress-load-bar-bg-color: rgba(0, 0, 0, 0.11);
@progress-load-bar-bg-color-light: rgba(0, 0, 0, 0.06);
@progress-bar-bg-color-light: rgba(0, 0, 0, 0.09);
@progress-bar-bg-color-dark: #fff;
@player-lyric-color: @primary-color;
@player-lyric-font-size: 36px;
@player-lyric-font-size-mobile: 16px;
@player-lyric-z-index: 999;

Customize in webpack

// webpack.config.js

module.exports = {
  rules: [{
    test: /\.less$/,
    use: [
      ...
      {
      loader: 'less-loader',
+     options: {
+       modifyVars: {
+         'primary-color': '#444',
+         // or
+         'hack': `true; @import "your-less-file-path.less";`, // Override with less file
+       },
+       javascriptEnabled: true,
+     },
    }],
  }],
}

Customize in less file

@import 'react-jinke-music-player/lib/styles/index.less';
@import 'your-theme-file.less';

:bulb: Customize audio duration

Default: by this.audio.duration, if cannot get current play audio's duration, you can customize to set.

<ReactJkMusicPlayer audioLists={[{
  ...
  duration: 100.00
}]} />

:bulb: Customize player icon

export interface ReactJkMusicPlayerIcon {
  pause?: React.ReactNode | string
  play?: React.ReactNode | string
  destroy?: React.ReactNode | string
  close?: React.ReactNode | string
  delete?: React.ReactNode | string
  download?: React.ReactNode | string
  toggle?: React.ReactNode | string
  lyric?: React.ReactNode | string
  volume?: React.ReactNode | string
  mute?: React.ReactNode | string
  next?: React.ReactNode | string
  prev?: React.ReactNode | string
  playLists?: React.ReactNode | string
  reload?: React.ReactNode | string
  loop?: React.ReactNode | string
  order?: React.ReactNode | string
  orderLoop?: React.ReactNode | string
  shuffle?: React.ReactNode | string
  loading?: React.ReactNode | string
}

:bulb: Follow the theme of the system

<ReactJkMusicPlayer theme="auto" />

auto-theme

:bulb: Quiet update

/**
 * Don't interrupt current playing state when audio list updated
 * eg. (A) is current playing...
 * [A,B] => [A,C,B]
 * [A,B] => [A,B,C]
 *
 * if (A) not in updated audio lists
 * [A,B] => [C]
 * (C) is playing
 */

function App() {
  const [audioLists, setAudioLists] = useState([
    { musicSrc: 'A' },
    { musicSrc: 'B' },
  ])

  useEffect(() => {
    setTimeout(() => {
      setAudioLists([{ musicSrc: 'A' }, { musicSrc: 'C' }, { musicSrc: 'B' }])
    }, 1000)
  }, [setAudioLists])

  return (
    <ReactJkMusicPlayer
      quietUpdate
      clearPriorAudioLists
      audioLists={audioLists}
    />
  )
}

:bulb: Import in browser

<!DOCTYPE html>
<html lang="en">
  <head>
    <link
      rel="stylesheet"
      href="https://unpkg.com/react-jinke-music-player@4.18.1/assets/index.css"
    />
  </head>
  <body>
    <div id="root"></div>
  </body>
  <script src="https://unpkg.com/react@16.13.1/umd/react.production.min.js"></script>
  <script src="https://unpkg.com/react-dom@16.13.1/umd/react-dom.production.min.js"></script>
  <script src="https://unpkg.com/react-jinke-music-player@4.18.1/dist/react-jinke-music-player.min.js"></script>
  <script>
    ReactDOM.render(
      React.createElement(ReactJkMusicPlayer),
      document.getElementById('root'),
    )
  </script>
</html>

:bulb: Server-Side Rendering

// components/Player.js
import React from 'react'
import ReactJkMusicPlayer from 'react-jinke-music-player'
import styles from 'react-jinke-music-player/assets/index.module.css'

export const Player = () => <ReactJkMusicPlayer />

// pages/_app.js
import dynamic from 'next/dynamic'
const PlayerWithNoSSR = dynamic(() => import('../components/Player'), {
  ssr: false,
})

:bulb: Customize mobile media query

eg. Use mobile UI on a iPad device

// Default '(max-width: 768px) and (orientation : portrait)'
<ReactJkMusicPlayer mobileMediaQuery="(max-width: 1024px)" />

:bulb: Audio volume fade in and fade out

<ReactJkMusicPlayer volumeFade={{ fadeIn: 500, fadeOut: 500 }} />

:pencil: Development

git clone https://github.com/lijinke666/react-music-player.git
yarn # npm install
yarn start # npm start
open `http://localhost:8084/`

:hourglass: Test case

npm run test

:bulb: AudioListProps

Like This

interface ReactJkMusicPlayerAudioListProps {
  name: string | React.ReactNode,
  musicSrc: string | () => Promise<string>,
  cover: string,
  singer?: string | React.ReactNode,
  duration?: number,
  lyric?: string,
  [key: string]: any
}>

:bulb: AudioInfo

Like This

interface ReactJkMusicPlayerAudioInfo {
  cover: string
  currentTime: number
  duration: number
  ended: boolean
  musicSrc: string
  muted: boolean
  name: string
  networkState: number
  paused: boolean
  played: any
  readyState: number
  startDate: any
  volume: number
  lyric: string
  [key: string]: any
}

:two_men_holding_hands: Contributors

Special thanks: @JeffreyCA

https://github.com/lijinke666/react-music-player/graphs/contributors

:page_facing_up: License

MIT

4.24.2

3 years ago

4.24.0

4 years ago

4.24.1

4 years ago

4.23.0

4 years ago

4.22.1

4 years ago

4.22.0

4 years ago

4.21.2

4 years ago

4.21.1

4 years ago

4.21.0

4 years ago

4.20.1

4 years ago

4.20.0

4 years ago

4.19.1

4 years ago

4.19.0

4 years ago

4.18.3

4 years ago

4.18.2

4 years ago

4.18.1

4 years ago

4.18.0

4 years ago

4.17.1

4 years ago

4.17.0

4 years ago

4.16.5

4 years ago

4.16.4

4 years ago

4.16.3

4 years ago

4.16.2

4 years ago

4.16.0

4 years ago

4.16.1

4 years ago

4.15.5

4 years ago

4.15.4

4 years ago

4.15.3

5 years ago

4.15.2

5 years ago

4.15.0

5 years ago

4.15.1

5 years ago

4.14.2

5 years ago

4.14.1

5 years ago

4.14.0

5 years ago

4.13.1

5 years ago

4.13.0

5 years ago

4.12.1

5 years ago

4.12.0

5 years ago

4.11.2

5 years ago

4.11.0

5 years ago

4.11.1

5 years ago

4.10.1

5 years ago

4.10.0

5 years ago

4.9.0

5 years ago

4.8.1

5 years ago

4.8.0

5 years ago

4.7.2

5 years ago

4.7.1

5 years ago

4.7.0

5 years ago

4.6.1

5 years ago

4.6.0

5 years ago

4.5.0

5 years ago

4.4.7

5 years ago

4.4.6

5 years ago

4.4.5

5 years ago

4.4.4

5 years ago

4.4.3

5 years ago

4.4.2

5 years ago

4.4.1

5 years ago

4.4.0

5 years ago

4.3.7

5 years ago

4.3.6

5 years ago

4.3.5

6 years ago

4.3.4

6 years ago

4.3.3

6 years ago

4.3.2

6 years ago

4.3.1

6 years ago

4.3.0

6 years ago

4.2.3

6 years ago

4.2.2

6 years ago

4.2.1

6 years ago

4.2.0

6 years ago

4.1.1

6 years ago

4.1.0

6 years ago

4.0.4

6 years ago

4.0.3

6 years ago

4.0.2

7 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.5.0

7 years ago

3.4.2

7 years ago

3.4.1

7 years ago

3.4.0

7 years ago

3.3.2

7 years ago

3.3.1

7 years ago

3.3.0

7 years ago

3.2.2

7 years ago

3.2.1

7 years ago

3.2.0

7 years ago

3.1.5

7 years ago

3.1.4

7 years ago

3.1.3

7 years ago

3.1.2

7 years ago

3.1.1

7 years ago

3.1.0

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.4.0

7 years ago

2.2.2

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.1.2

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

0.3.3

7 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago