skip to Main Content

I am trying to create a bird recognition application, so when I activate the recognition function, from the UI, I wish to be able to send it to the server in real-time for it to recognize the bird by its songs.
Now I can only send it after I stop the recording.

I haven’t been able to find any information online regarding this, or any work-around for it, yet.

Here is the recording function:


  async function startRecording() {
    try {
      const perm = await Audio.requestPermissionsAsync();
      if (perm.status === "granted") {
        await Audio.setAudioModeAsync({
          allowsRecordingIOS: true,
          playsInSilentModeIOS: true
        });
        const recordingInstance = new Audio.Recording();
        await recordingInstance.prepareToRecordAsync(Audio.RECORDING_OPTIONS_PRESET_HIGH_QUALITY);
        setRecording(recordingInstance);

      recordingInstance.setOnRecordingStatusUpdate((status) => {
        console.log("Base64 recording data:", status);
        console.log("uri: ", recordingInstance.getURI());
      });
        await recordingInstance.startAsync();

      }
    } catch (err) {
      console.error('Failed to start recording', err);
    }
  }

Has anyone done this before using expo? Any other suggestions are welcomed!
Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    I managed to do it opening the file and sending chunked data with a socket. The main problem was making sure the data was sent correctly, if you want to use this please be patient with the audio recording, you need to wait a few seconds before sending it.

    async function startRecording() {
    
      try {
       const perm = await Audio.requestPermissionsAsync();
       if (perm.status === "granted") {
         await Audio.setAudioModeAsync({
         allowsRecordingIOS: true,
         playsInSilentModeIOS: true
      });
    
      const recordingInstance = new Audio.Recording();
      await recordingInstance.prepareToRecordAsync(Audio.RECORDING_OPTIONS_PRESET_HIGH_QUALITY);
      await recordingInstance.startAsync();
    
      setRecording(recordingInstance);
      setRecordingBackLog(prevBackLog => [...prevBackLog, recordingInstance]);
    
      await uploadChunksToServer(recordingInstance, 24, 100);
    
      recordingInstance.setOnRecordingStatusUpdate(async (status) => {
      });
    
       }
     } catch (err) {
       console.error('Failed to start recording', err);
     }
    }
    

    Sending data:

     export async function uploadChunksToServer(recordingInstance, chunkSize, 
     delayBetweenChunks) {
     console.log('calling sending');
    
     let info = await FileSystem.getInfoAsync(recordingInstance.getURI());
     let uri = info.uri;
     let currentPosition = 0;
     await sleep(5000);
     let current_file_size = info.size;
     let prev_pos = 0;
    
    do{
    
      try{
    
      let info = await FileSystem.getInfoAsync(recordingInstance.getURI());
      current_file_size = info.size;
    
        if (currentPosition + chunkSize >= current_file_size &&  currentPosition === prev_pos && prev_pos !== 0){
          console.log('blocked')
          continue;
    
        }
        else{
          console.log(currentPosition, current_file_size);
          const fileChunk = await FileSystem.readAsStringAsync(uri, {
              encoding: FileSystem.EncodingType.Base64,
              position: currentPosition,
              length: chunkSize
            })
            currentPosition += chunkSize;
            socket.emit('audioData', fileChunk);
          }
          prev_pos = currentPosition;
    
    
    }
    catch (e) {
      console.log(e);
    }
    if (recordingInstance._isDoneRecording && current_file_size - currentPosition < chunkSize){
          const fileChunk = await FileSystem.readAsStringAsync(uri, {
          encoding: FileSystem.EncodingType.Base64,
          position: currentPosition,
          length: current_file_size - currentPosition
        })
        currentPosition += current_file_size - currentPosition;
        socket.emit('audioData', fileChunk);
        break
      }
    }while(currentPosition < current_file_size)
    console.log("final report >> ", currentPosition, current_file_size)
    console.log('exiting')
    
    }
    

  2. You can try websockets, it is used for real time server connection, without the need to call apis. You may find what you are looking for in this link
    https://socket.io

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search