The audio.play() HTML element fails to function in Chrome, preventing the audio from playing

I'm experiencing an issue with playing audio in Chrome when the audio.src is not called before the play call, but Firefox seems to handle it fine. Does anyone have any suggestions? You can check out the fiddle link below -

http://jsfiddle.net/vn215r2d/1/

You can also view the code here -

<html>
   <head>
      <script language="javascript">      
          var audioo = document.createElement("audio");

          function newCall() {        
              audioo.src = "";
              audioo.src = "http://xx.xx.xx.xx:8181/api/captcha/sound";
              audioo.play();               
          } 

          function playAgain() {               
               audioo.play(); // doesn't work in chrome :(
          } 

      </script>
   </head>
   <body>
   <button type="button" onClick="newCall()">New Captcha Call</button>
   <br>
   <button type="button" onClick="playAgain()">Replay Captcha</button>   
</body>
</html>

Answer №1

Insight

Wow, it seems that the issue lies in the audio.currentTime not being writable until the src is rewritten. Here's a workaround that actually works:

function resetAudio() {  
    audioo.src = "http://xx.xx.xx.xx:8181/api/captcha/sound";
    // Resetting the src makes all audio attributes writeable again
    audioo.src = audio.src;             
} 

I had to do some searching to find this solution, and I found it on this Stack Overflow thread:

Original Method

I experimented with resetting the audio position in Chrome, and it worked successfully. It's recommended to pause first as a precaution, but it should work regardless.

var audioo = document.createElement("audio");

// Add an event listener to play when the audio is ready
audioo.addEventListener("canplaythrough", function(){
    audioo.play(); 
}, false); 

function resetAudio() {        
     audioo.src = "";
     audioo.src = "http://xx.xx.xx.xx:8181/api/captcha/sound";             
} 

function playAudioAgain() { 
     if(audioo.readyState === 4){                       
         audioo.pause();       
         audioo.currentTime = 0;            
         audioo.play(); 
     } else {
         alert("Audio not ready yet.");
     }
} 

Check out these handy resources for more information:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the issue with ng-show in Angular?

When a user clicks on an icon, I want to display a drop-down menu. I attempted to use the ng-show directive for this purpose, but unfortunately, the drop-down menu is not appearing. I created an icon ..., and when clicked, I attempted to open the drop-do ...

Refresh a div with Ajax once all data has been fully loaded

I am currently using an ajax refresh div function that reloads a specific div every 10 seconds. Occasionally, the div loads before receiving data from mysql. Is there a way to modify it so that it waits 2 seconds after reloading the div? <script type ...

I am looking to send a combination of string and HTML data to the controller when using the Summernote editor in MVC

As a beginner in MVC development, there are certain aspects that I am still struggling with. Currently, I am using the Summernote Editor to compose blog posts and Ajax to submit them. However, I encountered an issue when trying to send both HTML data from ...

Display a div using Jquery when hovering over it with several classes

I want to create a hover effect where div2 fades in slowly when hovering over div1 within the same container. However, my jQuery code doesn't seem to be working as expected... $(".div").hover(function() { $(this).find(".div2").animate({ opac ...

Scrolling through four limited list items automatically

Hey there! I am currently working on a design project. You can check it out here. I'm trying to replicate a section from another site, which you can see here. <div class="latest-winners-container"> <h3 class="header">Latest Winners< ...

Run the Ionic function only when the app is launched for the first time

I'm facing an issue with a function in Ionic storage that sets an array to default values. I only want this function to run the first time the app is launched on a user's phone, but currently it runs every time the app is opened because it's ...

Utilizing Vue.js to dynamically update input fields based on other field values

I'm a beginner with Vue.js and Vuetify.js, and I am encountering some difficulties with the following: <v-select v-model="car.type" :items="types" label="Type" ></v-select> <div v-if="car.type === 'fiat'"> < ...

How can I extract and display chosen values from multiple dropdown menus in designated text boxes based on their unique identifiers?

A website for evaluating car values is in the works, using a combination of PHP, MYSQL, JQUERY, and JavaScript. The database includes 3 tables: Table "car_make": id make 1 BMW Table "model": model_id model_name make_id 1 M3 1 Table "ca ...

Using Javascript, send text from a textbox to an ActionResult in ASP.NET MVC using AJAX

Html <input type="password" id="LoginPasswordText" title="Password" style="width: 150px" /> <input type="button" id="LoginButton1" value="Save" class="LoginButton1Class" onclick="LoginButton1OnClick" /> Json var TextBoxData = { Text: Login ...

wordpress widget that triggers a function when a click event occurs on jquery

I'm facing an issue where this function works perfectly in my header, but seems to have no effect when used in a widget $("#myID").click(function(){ var name = $("#name").val(); var phone = $("#phone").val(); console.log(name +" "+ phone) ...

AngularJS Application/Controller Secured with Bulletproof Encapsulation

As a ServiceNOW Developer, I have encountered an interesting issue. ServiceNOW has transitioned from using Jelly based Content Management System to an AngularJS based Service Portal. The new interface utilizes AngularJS widgets for data manipulation and di ...

ng-bind-html not refreshed following ng-click triggering

Recently, I started learning about the MEAN stack. I have implemented an ng-repeat in my HTML code that displays a list of titles. Each title has an ng-click function attached to it, which is supposed to show more details in an overlay popup. blogApp.co ...

Tips for seamlessly incorporating React Epub Reader into your next js project

Having some trouble integrating the react epub reader with Next Js. It's working perfectly fine with react js, but I keep encountering an error. Can you help me figure out how to integrate the epub reader with next js? Here is the error This is my ...

New update: Adjusting the chart by using the slider to modify the date values on the x-axis

Update: I have made changes to the question, the issue with values not appearing on the x-axis has been resolved. Now, as the slider is moved, the data on the graph remains constant and I can see changing x values. I am currently working on incorporating ...

Activate the Alert (https://material-ui.com/components/alert/#alert) starting from the React component at the bottom of the hierarchy

When it comes to alerts, a normal alert is typically used like alert("message to be displayed");. However, I prefer using material UI Alerts which return a JSX component. For example: <Alert severity="success">This is a success alert — check it out ...

Utilize .GLB and Blender file formats within a Gridsome project developed with Vue.js

I am working on a project using Three.js in Gridsome, but I am facing difficulties importing .glb files (3D models) with GLTFLoader. While Gridsome recognizes img (png/jpg) or .js files stored in src/assets, it does not seem to support glb files. This is ...

Switching background images for DIVs when links are rolled over

Having trouble changing the background image of a div when a user hovers over a link. Any ideas on what might be causing this issue? <style> #divToSwap { background-image: url(black.jpg); height: 150px; width: 150px; } </style> &l ...

When using the Parse JS SDK with AngularJS UI routing, the login functionality may not properly retain the current user's

Below is the configuration and initialization of my module: angular.module('FXBApp', [ 'ui.bootstrap' ,'ui.router' ,'oc.lazyLoad' ,'parse-angular' ]). config(['$urlRouterProvider','$statePro ...

Implementing URL routing and error handling in a Node.js application using the Express framework

As a newcomer to node.js, I appreciate your patience. I've been experimenting with node.js and the express module. I had an idea for handling browser requests and now I have a simple question: Is this approach good practice, or is there a better sol ...

How many logical lines of code are in the Ubuntu operating system?

As I develop my web application, it is crucial for me to track the lines of code written in languages such as php, css, html, and JavaScript specific to the /var/www directory. However, when using the command line code counter tool, I find myself tempted ...