'AutoPlay audio in asp.net webForm not working

I want to autoplay audio on Timer Ticked events in asp.net webform but it's not working. However, if I click the play button and play the audio once the audio plays automatically on the next ticked events. What could be the issue here?

<form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
        <asp:Timer ID="Timer1" runat="server" Interval="3000" OnTick="Timer1_Tick"> 
         </asp:Timer>
    </div>
</form>

And here is the code

protected void Page_Load(object sender, EventArgs e)
{
 
}

protected void Timer1_Tick(object sender, EventArgs e)
{
    string link = "Audio/" + "1.mp3";
    link = "<audio Controls autoplay ><Source src=" + link + " " + "type= audio/mpeg></Video>";
    Literal1.Text = link;
}


Solution 1:[1]

You need to use UpdatePanel to be able to work with Timer as described in documentation:

The Timer control enables you to perform postbacks at a specified interval. When you use the Timer control as a trigger for an UpdatePanel control, the UpdatePanel control is updated by using an asynchronous, partial-page update. You must include a ScriptManager object in a Web page to use the Timer control.

You use the Timer control to update an UpdatePanel control by including the timer inside the UpdatePanel control. Alternatively, you can place the timer outside the UpdatePanel control and set the timer as a trigger.

You can also initiate full postback of a complete Web page by including the Timer control in the Web page and not setting it as a trigger for an UpdatePanel control.

So, your code should look like this:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="3000" OnTick="Timer1_Tick" /> 

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <div>
            <asp:Literal ID="Literal1" runat="server"></asp:Literal>
        </div>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
</asp:UpdatePanel>

See also: How to use timer control in asp.net?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1