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

246
Visualizações
JSRuntime not playing sound when initializing Blazor web app

So I have this simple page:

@page "/"
@inject IJSRuntime _jsRuntime

<button @onclick="MakeSound">Make sound</button>
<button @onclick="ShowPopUp">Show pop-up</button>

@code
{
    protected override void OnInitialized()
    {
        _jsRuntime.InvokeVoidAsync("makeSound");
        _jsRuntime.InvokeVoidAsync("showPopUp");
    }

    void MakeSound()
    {
        _jsRuntime.InvokeVoidAsync("makeSound");
    }

    void ShowPopUp()
    {
        _jsRuntime.InvokeVoidAsync("showPopUp");
    }
}

in which I have 2 buttons: one for playing a sound and one for showing an alert. They work fine but when I try to run these actions at initialization, only the alert message shows up and there's no sound. Why?

By the way, this is the JS, though I have a feeling that the problem is not here:

function makeSound() {
    var audio = new Audio("sounds/ding.wav");
    audio.play();
}

function showPopUp() {
    alert("Hello!");
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Calling JavaScript function in OnInitialized do not work, because DOM is not rendered yet, OnAftrRender is the right event to call JavaScript functions. So you can try like this

protected override void OnAfterRender(bool firstRender)
    {
       if(firstRender)
       { 
           _jsRuntime.InvokeVoidAsync("makeSound");
           _jsRuntime.InvokeVoidAsync("showPopUp");
       }
    }
about 4 years ago · Juan Pablo Isaza Relatório
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