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,aAAa,IAC7C,EAA2B,QAAU,CAAQ,GAC5C,IAEH,MAAO,CACL,OACA,QACA,OACA,SACA,YACA,eACA,iBACA,4BACA,0BAEJ,gBAEe","file":"/npm/expo-audio-hooks@1.0.3/src/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/expo-audio-hooks@1.0.3/src/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import { useCallback, useEffect, useMemo, useRef, useState } from 'react';\nimport { Audio } from 'expo-av';\n\nfunction useAudio(audioResource, settings = {}) {\n  const audioObject = useRef(null);\n  const currentAudioResource = useRef(null);\n  const nextAudioResource = useRef(null);\n  const isReadyForNextResource = useRef(true);\n  const [isLoadingAudio, setIsLoadingAudio] = useState(false);\n  const [isPlaying, setIsPlaying] = useState(false);\n  const isMounted = useRef(false);\n  const [bugWatchdogCounter, setBugWatchdogCounter] = useState(0);\n  const userOnPlaybackTimeUpdate = useRef(null);\n  const userOnPlaybackStatusUpdate = useRef(null);\n\n  // current status of the component\n  useEffect(() => {\n    isMounted.current = true;\n    return () => {\n      isMounted.current = false;\n    };\n  }, []);\n\n  // the expo-av bug watchdog\n  // expo-av has a few bugs that cause the audio to not play or pause when requested.\n  // this is a workaround that checks the audio status every n ms and force a play/pause action.\n  useEffect(() => {\n    const timerHandler = setInterval(() => {\n      setBugWatchdogCounter((value) => value + 1);\n    }, 500);\n\n    return () => clearInterval(timerHandler);\n  }, []);\n\n  // merge user settings with default settings\n  const componentSettings = useMemo(() => {\n    const defaultSettings = {\n      autoPlay: false,\n    };\n\n    return {\n      ...defaultSettings,\n      ...settings,\n    };\n  }, [settings]);\n\n  // check if the user has defined the audio resource\n  const isResourceDefined = useMemo(() => {\n    // if the audio resource is null or undefined, return false\n    if (!audioResource) {\n      return false;\n    }\n\n    // if the audio resource has a uri, it's a streaming audio\n    if ('uri' in audioResource) {\n      return !!audioResource.uri;\n    }\n\n    // if the audio resource is not null nor a streaming, it is a local file\n    return true;\n  }, [audioResource]);\n\n  // check if two audio resources are the same\n  function checkSameAudioResources(resource1, resource2) {\n    if (!resource1 || !resource2) {\n      return false;\n    }\n\n    const isResourcesStreaming = ('uri' in resource1) && ('uri' in resource2);\n    if (isResourcesStreaming) {\n      return resource1.uri === resource2.uri;\n    }\n\n    return resource1 === resource2;\n  }\n\n  // unload and free audio resources\n  const unload = useCallback(async () => {\n    // safe guard to not unload the audio object if not initialized\n    if (!audioObject.current) {\n      return;\n    }\n\n    // only call `stop` if the audio is loaded\n    const status = await audioObject.current.getStatusAsync();\n    if (status.isLoaded) {\n      await audioObject.current.stopAsync();\n    }\n\n    // unload the audio object\n    await audioObject.current.unloadAsync();\n\n    // check if component is still mounted before changing a state\n    if (!isMounted.current) {\n      return;\n    }\n\n    // clear the audio object\n    audioObject.current = null;\n    currentAudioResource.current = null;\n  }, []);\n\n  // unload audio when unmount the component\n  useEffect(() => unload, [unload]);\n\n  // play/pause the audio according to isPlaying status\n  useEffect(() => {\n    const playOrPause = async () => {\n      // safe guard to not call audio object if not initialized\n      const isAudioObjectDefined = !!audioObject.current;\n      if (!isAudioObjectDefined) {\n        return;\n      }\n\n      // safe guard to not call if in process of canceling current audio resource\n      if (!isReadyForNextResource.current) {\n        return;\n      }\n\n      // safe guard to not call if the audio is not loaded\n      // due to async, sometimes `isLoadingAudio` is false but the audio is not loaded yet\n      const status = await audioObject.current.getStatusAsync();\n      if (isLoadingAudio || !status.isLoaded) {\n        return;\n      }\n\n      // play/pause the audio\n      try {\n        if (isPlaying) {\n          await audioObject.current.playAsync();\n        } else {\n          await audioObject.current.pauseAsync();\n        }\n      } catch {\n        // bug on expo-av: status not always return correct value for `isLoaded`\n        // it's okay to ignore the exception as we would skip the play/pause action anyway\n      }\n    };\n    playOrPause();\n  }, [isLoadingAudio, isPlaying, bugWatchdogCounter]);\n\n  // onStatusUpdate callback\n  const onPlaybackStatusUpdate = useCallback((status) => {\n    // provides the current playback time\n    if (userOnPlaybackTimeUpdate.current) {\n      const positionMillis = status.positionMillis ?? 0;\n      const durationMillis = Number.isNaN(status.durationMillis) ? 1 : status.durationMillis ?? 1;\n      const remainingMillis = durationMillis - positionMillis;\n      userOnPlaybackTimeUpdate.current({ positionMillis, durationMillis, remainingMillis });\n    }\n\n    if (userOnPlaybackStatusUpdate.current) {\n      userOnPlaybackStatusUpdate.current(status);\n    }\n  }, []);\n\n  // create the audio object when the audio resource changes.\n  // if an audio object already exists, stops and unload it before creating a new one.\n  // because expo-av doesn't support canceling a loading resource, we implement a queue system.\n  // we put the new audio resource in a queue and process it when the previous one has\n  // finished loading and is destroyed.\n  useEffect(() => {\n    // wait until we have an audio source\n    if (!isResourceDefined) {\n      return;\n    }\n\n    // check if changed the audio resource.\n    // this effect is triggered on many different state changes.\n    // we only take action if the audio resource changed.\n    const isSameResource = checkSameAudioResources(currentAudioResource.current, audioResource);\n    if (isSameResource) {\n      return;\n    }\n\n    // put resource in queue to be loaded next.\n    const isAlreadyNext = checkSameAudioResources(nextAudioResource.current, audioResource);\n    if (isAlreadyNext) {\n      return;\n    }\n    nextAudioResource.current = audioResource;\n\n    // still processing the previous resource.\n    if (!isReadyForNextResource.current) {\n      return;\n    }\n\n    // function to load the next resource\n    async function createAudioObjectFromQueue() {\n      try {\n        // update loading status\n        setIsLoadingAudio(true);\n\n        // destroy current audio object\n        await unload();\n\n        // create the audio object\n        const sound = new Audio.Sound();\n        audioObject.current = sound;\n\n        // load the next audio resource\n        const initialStatus = {};\n        const downloadFirst = true;\n        sound.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate);\n        await sound.loadAsync(nextAudioResource.current, initialStatus, downloadFirst);\n\n        // always check if the component is still mounted on async operations\n        if (!isMounted.current) {\n          return;\n        }\n\n        // if the user wants to play the audio automatically, play it\n        if (componentSettings.autoPlay) {\n          setIsPlaying(true);\n        }\n      } finally {\n        // always check if the component is still mounted on async operations\n        if (!isMounted.current) {\n          return;\n        }\n\n        // update the queue and free it for next resources\n        currentAudioResource.current = nextAudioResource.current;\n        nextAudioResource.current = null;\n        isReadyForNextResource.current = true;\n\n        // update loading status\n        setIsLoadingAudio(false);\n      }\n    }\n\n    // user wants to change the audio resource while the previous one is still being loaded\n    // cancel the current loading and create a new audio object\n    if (isLoadingAudio) {\n      // expo-av limitation: there's no way to cancel an already loading audio resource\n      // as a workaround, we wait for the song be loaded and then we destroy it\n      // the best way to detect the song has finished loading, is through `onPlaybackStatusUpdate`\n      isReadyForNextResource.current = false;\n\n      // safe guard to not call the audio object if not initialized\n      if (!audioObject.current) {\n        return;\n      }\n\n      // wait song to be loaded, destroy it, and load the next in queue\n      audioObject.current.setOnPlaybackStatusUpdate(async () => {\n        await createAudioObjectFromQueue();\n      });\n      return;\n    }\n\n    // normal flow: user changed audio resource, no loading in progress.\n    createAudioObjectFromQueue();\n  }, [\n    audioResource,\n    componentSettings.autoPlay,\n    isLoadingAudio,\n    isResourceDefined,\n    onPlaybackStatusUpdate,\n    unload,\n  ]);\n\n  // change the audio position\n  const seek = useCallback(async (positionMs) => {\n    await audioObject.current.setPositionAsync(positionMs);\n  }, []);\n\n  // pause the audio\n  const pause = useCallback(() => {\n    setIsPlaying(false);\n  }, []);\n\n  // play the audio\n  const play = useCallback(() => {\n    setIsPlaying(true);\n  }, []);\n\n  // callbacks\n  const setOnPlaybackTimeUpdate = useCallback((callback) => {\n    userOnPlaybackTimeUpdate.current = callback;\n  }, []);\n\n  const setOnPlaybackStatusUpdate = useCallback((callback) => {\n    userOnPlaybackStatusUpdate.current = callback;\n  }, []);\n\n  return {\n    play,\n    pause,\n    seek,\n    unload,\n    isPlaying,\n    setIsPlaying,\n    isLoadingAudio,\n    setOnPlaybackStatusUpdate,\n    setOnPlaybackTimeUpdate,\n  };\n}\n\nexport default useAudio;\n"]}