@/utils#getRandom JavaScript Examples

The following examples show how to use @/utils#getRandom. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: action.js    From lx-music-mobile with Apache License 2.0 5 votes vote down vote up
playPrev = () => async(dispatch, getState) => {
  const { player, common } = getState()
  const currentListId = player.listInfo.id
  const currentList = player.listInfo.list
  const playInfo = playInfoGetter(getState())
  if (player.playedList.length) {
    let currentSongmid
    if (player.playMusicInfo.isTempPlay) {
      const musicInfo = currentList[playInfo.listPlayIndex]
      if (musicInfo) currentSongmid = musicInfo.songmid || musicInfo.musicInfo.songmid
    } else {
      currentSongmid = player.playMusicInfo.musicInfo.songmid || player.playMusicInfo.musicInfo.musicInfo.songmid
    }
    // 从已播放列表移除播放列表已删除的歌曲
    let index
    for (index = player.playedList.findIndex(m => (m.musicInfo.songmid || m.musicInfo.musicInfo.songmid) === currentSongmid) - 1; index > -1; index--) {
      const playMusicInfo = player.playedList[index]
      const currentSongmid = playMusicInfo.musicInfo.songmid || playMusicInfo.musicInfo.musicInfo.songmid
      if (playMusicInfo.listId == currentListId && !currentList.some(m => (m.songmid || m.musicInfo.songmid) === currentSongmid)) {
        dispatch({ type: TYPES.removeMusicFormPlayedList, payload: index })
        continue
      }
      break
    }

    if (index > -1) {
      dispatch(playMusic(player.playedList[index]))
      return
    }
  }

  let filteredList = await filterList({
    listInfo: player.listInfo,
    playedList: player.playedList,
    savePath: common.setting.download.savePath,
    dispatch,
  })

  if (!filteredList.length) return dispatch(playMusic(null))

  let listPlayIndex = playInfo.listPlayIndex
  const currentListLength = player.listInfo.list.length - 1
  if (listPlayIndex == -1 && currentListLength) {
    listPlayIndex = global.prevListPlayIndex >= currentListLength ? 0 : global.prevListPlayIndex + 1
  }
  let currentIndex = listPlayIndex
  if (currentIndex < 0) currentIndex = 0
  let nextIndex = currentIndex
  if (!playInfo.isTempPlay) {
    switch (common.setting.player.togglePlayMethod) {
      case MUSIC_TOGGLE_MODE.random:
        nextIndex = getRandom(0, filteredList.length)
        break
      case MUSIC_TOGGLE_MODE.listLoop:
      case MUSIC_TOGGLE_MODE.list:
      case MUSIC_TOGGLE_MODE.singleLoop:
      case MUSIC_TOGGLE_MODE.none:
        nextIndex = currentIndex === 0 ? filteredList.length - 1 : currentIndex - 1
        break
      default:
        nextIndex = -1
        return
    }
    if (nextIndex < 0) return
  }

  dispatch(playMusic({
    musicInfo: filteredList[nextIndex],
    listId: currentListId,
  }))
}
Example #2
Source File: action.js    From lx-music-mobile with Apache License 2.0 4 votes vote down vote up
playNext = isAutoToggle => async(dispatch, getState) => {
  const { player, common } = getState()
  if (player.tempPlayList.length) {
    const playMusicInfo = player.tempPlayList[0]
    dispatch(removeTempPlayList(0))
    dispatch(playMusic(playMusicInfo))
    return
  }
  const currentListId = player.listInfo.id
  const currentList = player.listInfo.list
  const playInfo = playInfoGetter(getState())
  if (player.playedList.length) {
    let currentSongmid
    if (player.playMusicInfo.isTempPlay) {
      const musicInfo = currentList[playInfo.listPlayIndex]
      if (musicInfo) currentSongmid = musicInfo.songmid || musicInfo.musicInfo.songmid
    } else {
      currentSongmid = player.playMusicInfo.musicInfo.songmid || player.playMusicInfo.musicInfo.musicInfo.songmid
    }
    // 从已播放列表移除播放列表已删除的歌曲
    let index
    for (index = player.playedList.findIndex(m => (m.musicInfo.songmid || m.musicInfo.musicInfo.songmid) === currentSongmid) + 1; index < player.playedList.length; index++) {
      const playMusicInfo = player.playedList[index]
      const currentSongmid = playMusicInfo.musicInfo.songmid || playMusicInfo.musicInfo.musicInfo.songmid
      if (playMusicInfo.listId == currentListId && !currentList.some(m => (m.songmid || m.musicInfo.songmid) === currentSongmid)) {
        dispatch({ type: TYPES.removeMusicFormPlayedList, payload: index })
        continue
      }
      break
    }

    if (index < player.playedList.length) {
      dispatch(playMusic(player.playedList[index]))
      return
    }
  }

  let filteredList = await filterList({
    listInfo: player.listInfo,
    playedList: player.playedList,
    savePath: common.setting.download.savePath,
    dispatch,
  })

  // console.log(filteredList)
  if (!filteredList.length) return dispatch(playMusic(null))
  let listPlayIndex = playInfo.listPlayIndex
  const currentListLength = player.listInfo.list.length - 1
  if (listPlayIndex == -1 && currentListLength) {
    listPlayIndex = global.prevListPlayIndex > currentListLength ? currentListLength : global.prevListPlayIndex - 1
  }
  const currentIndex = listPlayIndex
  let nextIndex = currentIndex
  let togglePlayMethod = common.setting.player.togglePlayMethod
  if (isAutoToggle !== true) {
    switch (togglePlayMethod) {
      case MUSIC_TOGGLE_MODE.list:
      case MUSIC_TOGGLE_MODE.singleLoop:
      case MUSIC_TOGGLE_MODE.none:
        togglePlayMethod = MUSIC_TOGGLE_MODE.listLoop
    }
  }
  switch (togglePlayMethod) {
    case MUSIC_TOGGLE_MODE.listLoop:
      nextIndex = currentIndex === filteredList.length - 1 ? 0 : currentIndex + 1
      break
    case MUSIC_TOGGLE_MODE.random:
      nextIndex = getRandom(0, filteredList.length)
      break
    case MUSIC_TOGGLE_MODE.list:
      nextIndex = currentIndex === filteredList.length - 1 ? -1 : currentIndex + 1
      break
    case MUSIC_TOGGLE_MODE.singleLoop:
      break
    default:
      nextIndex = -1
      break
  }

  if (nextIndex < 0) {
    dispatch(setStatus({ status: STATUS.stop, text: i18n.t('stopped') }))
    lrcPause()
    return
  }

  dispatch(playMusic({
    musicInfo: filteredList[nextIndex],
    listId: currentListId,
  }))
}