Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
Three.js LoadingManager callback function is not playing loaded audio file

I can't understand why the audio file won't play in the LoadingManager callback function. It's completely illogical to me. I hope somebody can explain it to me. I stripped my code to only load a single mp3 file and play it after loading is complete.

In the console I get this error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'play')

index.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <title>Test</title>
    <script type="importmap">
        {
            "imports": {
                "three": "../threejs/build/three.module.js"
            }
        }
    </script>

    <script type="module" src="main2.js"></script>    
</head>
<body>
  
<style>
body { 
 margin: 0;
}
</style>

<div id="overlay">
    <button id="startButton">Play</button>
</div>

</body>
</html>

main2.js:

import * as THREE from 'three';

let camera, scene, renderer, sndMusic1;

// SCENE
scene = new THREE.Scene();

// CAMERA
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1500 );

// RENDERER
renderer = new THREE.WebGLRenderer( { antialias: true, alpha: false } );

document.body.appendChild(renderer.domElement);

const manager = new THREE.LoadingManager();
manager.onStart = function ( url, itemsLoaded, itemsTotal ) {
    console.log( 'Started loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );
};

const audioLoader = new THREE.AudioLoader(manager);

const startButton = document.getElementById( 'startButton' );
startButton.addEventListener( 'click', init );

manager.onProgress = function ( url, itemsLoaded, itemsTotal ) {
    console.log( 'Loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' );
};

manager.onLoad = function () {  // LoadingManager callback function
    console.log('All files loaded.');
    sndMusic1.play();
}

function init() {
    const overlay = document.getElementById( 'overlay' );
    overlay.style.display = "none";  // Hide the start button
    
    const listener = new THREE.AudioListener();
    camera.add( listener );

    audioLoader.load( './sound/arise5.mp3', function( buffer ) {
        sndMusic1 = new THREE.Audio( listener );
        sndMusic1.setBuffer( buffer );
    });
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda