Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8461909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:00:42+00:00 2026-06-10T14:00:42+00:00

I have a problem where I can play an audio file if it is

  • 0

I have a problem where I can play an audio file if it is added directly to the html5 source code as the src attribute, but it doesn’t play if I try to set the src at runtime.

Originally I had the following piece of HTML containing a few audio tags:

<audio id="audio1" controls preload="none" src="http:/theurl/clip1.mp3"></audio>
<audio id="audio2" controls preload="none" src="http:/theurl/clip2.mp3"></audio>

With the clips being played with the following function that gets called with the id and clip url:

function audioClick(id, clip) {
    var audio = document.getElementById(id);
    if (audio.paused) {
        audio.play();
    }
    else {
        audio.pause();
        audio.currentTime = 0;
    }
}

This works and when the user clicks on the relevant part of the page the appropriate audio file plays, however the problem is that the browser takes about 3-4 seconds to load the page which is too long. I have tracked down the reason for the delay to be due to the src attribute being remote files. And setting preload to “none” or “metadata” doesn’t do anything to speed things up.

So to workaround this I wanted to set the src at runtime, so changed the function to this:

function audioClick(id, clip) {
    var audio = document.getElementById(id);
    if (audio.paused) {
        audio.setSrc(clip);
        audio.load();
        audio.play();
    }
    else {
        audio.pause();
        audio.currentTime = 0;
    }
}

I tried setting the html to this

<audio id="audio1" controls></audio>
<audio id="audio2" controls></audio>

But there were two problems:
1) the audio doesn’t play
2) the controls displays an initial message saying the file can’t be played

I don’t there to be a message in the controls saying the file can’t be played so I changed the html to:

<audio id="audio1"></audio>
<audio id="audio2"></audio>

However the problem with the file not playing remains.

Why doesn’t the audio file play when done this way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:00:44+00:00Added an answer on June 10, 2026 at 2:00 pm

    try something like:

    function audioClick(id, src) {
      var audio = document.getElementById(id);
      if (audio.paused) {
        audio.addEventListener('canplay', function canplay() {
          this.removeEventListener('canplay', canplay, false);
          this.play();
        }, false);
    
        audio.src = src;
      }
      else {
        audio.pause();
        audio.currentTime = 0;
      }
    }
    

    and have a look at other available media events.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following code to play audio file. I have tested the
i have a problem that i can't solve ! (sqlite3, but i think it
Alright I have two problems. I'm using AVAudioPlayer to play a simple audio file
I have a problem in determining when the audio.play() method done in playing mp3
I have a problem, when I try to record and then play the file
I have one problem with my iOS Application. I want to play multiple audio
I have prepared a code to just play a simple mp4 file from my
I am using a simple bit of code to play an audio file: NSString
I have the following javascript code to play some HTML5 sounds: var html5_audiotypes={ //define
I have three mp3 audio files that play fine in all browsers but play

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.