0

I have been trying for a couple days just to be able to get a sound to play on my iOS simulator (currently using XCode's Simulator app). All I'm looking for is to be able to press the "Pressable" component, and trigger audio playback. I have run npx expo update to ensure that all of my packages are up to date. I have also ensured that the path to my audio file is correct.

This is the code I have currently:

import { View, Text, Pressable } from 'react-native'
import React, { useEffect, useRef, useState } from 'react'

import { Audio } from 'expo-av';


export default function TrackBody({trackIndex, contents, isPlaying, updateTrackContents}) {

    const soundRef = useRef(null);

    useEffect(() => {
        
        const loadSounds = async () => {
            try {
                const sound = new Audio.Sound();
                await sound.loadAsync(require("../assets/audio/samples/audio.mp3"));
                console.log('Sound Loaded Successfully');
                console.log(sound);
                soundRef.current = sound;
                console.log("Playback status1:", await soundRef.current.getStatusAsync());
            } catch (error) {
                console.log("Error loading sound:", error);
            }
            
        };
        loadSounds();

        return () => {
            console.log('Sound Unloading')
            soundRef.current && soundRef.current.unloadAsync();
            console.log(soundRef.current)
        };
    }, []);

    const testSound = async () => {

        try {
            if (soundRef.current) {
                const status = await soundRef.current.getStatusAsync();
                await soundRef.current.playAsync();
                console.log('here3')
            } else {
                console.log("Sound reference is null: ", soundRef)
            }
        } catch (error) {
            console.log(error)
        }
    }

  return (
    <View>
        <Pressable style={{backgroundColor: 'red', height: 40, width: 40}} onPress={async () => await testSound()}></Pressable>
    </View>
  )
}

Am I making some sort of glaring mistake? No matter how many videos I follow, or how many times I replicate the code from the docs precisely, I am unable to get audio to play using this package.

  • can you `console.log(status)` after `const status = await soundRef.current.getStatusAsync()` – Yilmaz Jun 26 '23 at 03:29

0 Answers0