Expo-AV How to execute code after a Audio finishes?

Please provide the following:

  1. SDK Version: 3.11.7
  2. Platforms(Android/iOS/web/all): Android

I’m a new Dev so please bear with me!

I been assigned a task on a react-native application using ‘expo-av’ library for Audio files.

Right now the app has a file called MusicContext.js with the following code:

import React, { useState, useEffect } from "react"
import PropTypes from "prop-types"
import { Audio } from "expo-av"

const initialState = {
  startMusic: () => null,
  stopMusic: () => null,
  soundObject: null,
}

export const MusicContext = React.createContext(initialState)

export const MusicProvider = props => {
  const [gameSoundsPlay, setGameSoundsPlay] = useState(
    initialState.mainThemePlay
  )

  const [soundObject, setSoundObject] = useState(initialState.mainThemePlay)

  const startMusic = async () => {
    mainTheme = new Audio.Sound()

    try {
      await mainTheme.loadAsync(require("../assets/sounds/Shoe.mp3"))
      await mainTheme.setStatusAsync({ isLooping: true })
      await mainTheme.playAsync()
      setSoundObject(mainTheme)
    } catch (error) {
      console.log("Couldnt load main theme")
      return
    }
  }

  const stopMusic = async () => {
    try {
      await soundObject.stopAsync()
      await soundObject.unloadAsync()
      setSoundObject(null)
    } catch (error) {
      console.log("Couldnt stop main theme")
      return
    }
  }

As it is, the app simply plays that background song Shoe.mp3 on a repeated loop ( thanks to await mainTheme.setStatusAsync({ isLooping: true }) )

My task is to add other audio files, and have them play after this. However, before that I want to simply console log WHEN THAT FIRST SONG FINISHES

I deleted await mainTheme.setStatusAsync({ isLooping: true }), and wrote

if (mainTheme.didJustFinish) {
  console.log("Main theme song has just ended")
}

But nothing logs. I figure if I can figure this out, I can figure out when/where/how to place code so that it will play the next Audio upon completion of the first…

I hope this makes sense! Thanks all

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.