transistorsoft / react-native-background-geolocation

Sophisticated, battery-conscious background-geolocation with motion-detection
http://shop.transistorsoft.com/pages/react-native-background-geolocation
MIT License
2.65k stars 426 forks source link

[ANDROID] Audio recording doesn't work with Background Geolocation #1019

Closed JordanProtin closed 4 years ago

JordanProtin commented 4 years ago

Your Environment

Expected Behavior

Allow audio recording

Actual Behavior

My app is a Google Assistant Voice like. Indeed, I can speech a specific hotword and my app is able to detect this (it's a listener which listen all of time). But when BackgroundGeolocation is starting, my audio recognition doesn't work. Maybe your plugin disables audio recording.. ?

When I comment BackgroundGeolocation calls, my audio recognition works well.

Steps to Reproduce

Context

Debug logs

christocracy commented 4 years ago

Maybe your plugin disables audio recording.. ?

No, it does not, but it does play audio with debug sound FX. Perhaps you should debug: false

JordanProtin commented 4 years ago

Thank you for your quickly answer @christocracy ! I tried to set debug: false, but I get the same behavior

JordanProtin commented 4 years ago

Probably it's an error which is specific to my app if your plugin does play audio recording ! I try to resolve this and I'll post my solution if I found it. Thank you @christocracy for your great work !

christocracy commented 4 years ago

I would start by observing raw $ adb logcat for issues while attempting to execute audio recording. There must be some warning/error that stands out.

JordanProtin commented 4 years ago

Yes I'll try by using $ adb logcat.

Also, I would like your opinion about using React Hook with BackgroundGeolocation because I think I don't implement it in the best way.

In my current screen, I have two useEffect. One for configure BackgroundGeolocation and the other to start BackgroundGeolocation, like this :

const {
    configureBgGeo,
    startBgGeo,
    isReadyBgGeo,
    isInitialisingBgGeo,
    coords
  } = useBackgroundGeolocation();

useEffect(() => {
    if (!isInitialisingBgGeo) {
      configureBgGeo();
    }
  }, [ isInitialisingBgGeo]);

useEffect(() => {   
    if (!isReadyBgGeo && isInitialisingBgGeo) {
      startBgGeo();
    }
  }, [isReadyBgGeo, startBgGeo, isInitialisingBgGeo]);

And in my BackgroundGeolocation hook, there are these two functions :

const [isReadyBgGeo, setIsReadyBgGeo] = useState(false);
const [isInitialisingBgGeo, setIsInitialisingBgGeo] = useState(false);

const configureBgGeo = useCallback(
    async (config) => {
      setIsInitialisingBgGeo(true);

      BackgroundGeolocation.ready(
        {
          // Geolocation Config
          desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
          distanceFilter: 5,
          // Activity recognition
          stopTimeout: 1,
          // Application config
          debug: false, // <-- enable this hear sounds for background-geolocation life-cycle.   
          logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
          stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app.
          startOnBoot: true, // <-- Auto start tracking when device is powered-up.
          foregroundService: true,
          // config override
          ...config
        }, 
        (state) => {
          console.log('- BackgroundGeolocation is configured and ready: ', state.enabled);
          setIsReadyBgGeo(state.enabled);
        },
        (error) => {
          console.warn('- BackgroundGeolocation error: ', error);
          setIsInitialisingBgGeo(false);
        }
      );
    }, 
    []
  );

const startBgGeo = useCallback(
    () => {     
      BackgroundGeolocation.start(
        () => {
          console.log("- BackgroundGeolocation - Start success");
        },
        (error) => {
          console.log('- BackgroundGeolocation - Start error', error)
        }
      );
    }, 
    []
  );

It's the best way to use your plugin by using React Hook ?

JordanProtin commented 4 years ago

UPDATE : because I get this warning with $ adb logcat and I think it's my issue => 'Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn\'t have a dependency array, or one of the dependencies changes on every render.%s'

christocracy commented 4 years ago

Also, I would like your opinion about using React Hook with BackgroundGeolocation

I have no experience with hooks. You need to ensure the BackgroundGeolocation.ready gets called only once when the app boots. It is not to be called each time a render occurs. BackgroundGeolocation may be unsuitable for use with hooks.

JordanProtin commented 4 years ago

Hi @christocracy !

I was able to resolve my issue !

My bad, I have also no great experience with hooks and it's the reason of my encountered problems ! u.u

Indeed, as I said, I use a hook to start/stop BackgroundGeolocation plugin with all registered listeners. So, when I received a location update from onLocation event, I set a location in React State of my custom hook and returns as a prop.

Then, in my component, I can catch the new coordinates when this prop changes by using React useEffect. So I add these new coordinates to an array in React State always using ReactHooks like this :

useEffect(() => {
    if (coords) {
      let newCoordinates = {
        latitude: coords.latitude,
        longitude: coords.longitude
      }

      // add new user coordinates within a temporary array of objects
      if (!routeData.includes(newCoordinates)) {
        setRouteData(routeData => [...routeData, newCoordinates]);
      } 
    }
  }, [coords, routeData]); <-- the mistake

And the mistake was I put routeData in [] of the useEffect. Indeed, I got an infinity loop !!!! Therefore my app was freezing and I couldn't do anything about it anymore, like execute audio recording..

Thank you for your help and your quick feedback. Sorry to have questioned your work, your library is wonderful !

I close this issue