Reputation: 2641
I am implementing an AudioPlayer using the expo-av library. For playing audios I am doing this:
import { Audio } from "expo-av";
export default function AudioPlayer() {}
AudioPlayer.playSound = async function (
source,
initialStatus = { shouldPlay: true },
onPlaybackStatusUpdate = null,
downloadFirst = true
) {
try {
// Create a complete sound object
const { sound, status } = await Audio.Sound.createAsync(
source,
initialStatus,
onPlaybackStatusUpdate,
downloadFirst
);
console.log(status);
// Play the sound
await sound.playAsync();
console.log("Sound played! Unloading from memory...");
// We are done...
// Unload the sound from memory
sound.unloadAsync();
} catch (err) {
throw err;
}
};
But, for some reason, when I do
AudioPlayer.playSound(require("../../assets/microphone-button-press.mp3"));
no sound is played.
What am I doing wrong? (tested on iPhone 11 pro, iOS 14)
This is how the status object I am logging looks like:
Object {
"didJustFinish": false,
"durationMillis": 3120,
"hasJustBeenInterrupted": false,
"isBuffering": false,
"isLoaded": true, <---------------
"isLooping": false,
"isMuted": false, <------------
"isPlaying": false,
"pitchCorrectionQuality": "Varispeed",
"playableDurationMillis": 3120,
"positionMillis": 0,
"progressUpdateIntervalMillis": 500,
"rate": 1,
"shouldCorrectPitch": false,
"shouldPlay": true, <-----------------
"uri": "file:///var/mobile/Containers/Data/Application/E249E9F5-63EE-4421-88E6-F4F52B0D99F2/Library/Caches/ExponentExperienceData/%2540victorio%252Fmy-app-name/ExponentAsset-cd2511ab9f64faf7c2c23a26c8ac58c5.mp3", <----------------
"volume": 1, <--------------
}
Upvotes: 2
Views: 4234
Reputation: 375
I little bit old but in case some one get into this at 2023/2024 for newer versions. the solution posted here solved for me.
await _sound.loadAsync(require("./assets/sounds/sample.mp3"), {shouldPlay: true});
Upvotes: 0
Reputation: 314
Load your sound before playing it.
await sound.current.loadAsync(
Sounds[audioSourceWithRequire],
);
then await sound.current.playAsync();
Upvotes: 2