How to play audio?

play audio javascript
html audio
jquery play audio
javascript audio
html audio player code
html audio autoplay
javascript play sound onclick
html audio controls style

I am making a game with HTML5 and JavaScript.

How could I play game audio via JavaScript?

If you don't want to mess with HTML elements:

var audio = new Audio('audio_file.mp3');
audio.play();

function play() {
  var audio = new Audio('https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3');
  audio.play();
}
<button onclick="play()">Play Audio</button>

HTML DOM Audio play() Method, An audio player with play and pause buttons: var x = document.getElementById(" myAudio"); function playAudio() { x.play(); } function pauseAudio() { x.pause(); }. To do this, follow the next steps: Go to the Insert tab, click on Audio, and then choose Audio on My PC. If you’re using Office 2010 or an older version, Browse your computer for the file you want to play. Once you’ve found it, either double-click on it or select it and Under Audio Tools,

It's easy, just get your audio element and call the play() method:

document.getElementById('yourAudioTag').play();

Check out this example: http://www.storiesinflight.com/html5/audio.html

This site uncovers some of the other cool things you can do such as load(), pause(), and a few other properties of the audio element.

HTML Audio, The controls attribute adds audio controls, like play, pause, and volume. The < source> element allows you to specify alternative audio files which the browser may� To get the tab you need to click on the audio icon on the slide. The options for playback are divided under two groups of tools called Audio Options and Audio Styles. Audio options and Audio Styles Menu. First, let us explore the group under ‘Audio options’ in Playback tab.

http://www.schillmania.com/projects/soundmanager2/

SoundManager 2 provides a easy to use API that allows sound to be played in any modern browser, including IE 6+. If the browser doesn't support HTML5, then it gets help from flash. If you want stricly HTML5 and no flash, there's a setting for that, preferFlash=false

It supports 100% Flash-free audio on iPad, iPhone (iOS4) and other HTML5-enabled devices + browsers

Use is as simple as:

<script src="soundmanager2.js"></script>
<script>
    // where to find flash SWFs, if needed...
    soundManager.url = '/path/to/swf-files/';

    soundManager.onready(function() {
        soundManager.createSound({
            id: 'mySound',
            url: '/path/to/an.mp3'
        });

        // ...and play it
        soundManager.play('mySound');
    });
</script>

Here's a demo of it in action: http://www.schillmania.com/projects/soundmanager2/demo/christmas-lights/

How to play audio?, If you don't want to mess with HTML elements: var audio = new Audio('audio_file. mp3'); audio.play();. function play() { var audio = new� To fix this, right-click the speaker icon in the Windows taskbar and choose Sounds to enter the audio preferences. Under the Playback tab, find the device you want to use—if you don't see it, try

This is a quite old question but I wanna add some useful info. The topic starter has mentioned that he is "making a game". So for everybody who needs audio for game development there is a better choice than just an <audio> tag or an HTMLAudioElement. I think you should consider the use of the Web Audio API:

While audio on the web no longer requires a plugin, the audio tag brings significant limitations for implementing sophisticated games and interactive applications. The Web Audio API is a high-level JavaScript API for processing and synthesizing audio in web applications. The goal of this API is to include capabilities found in modern game audio engines and some of the mixing, processing, and filtering tasks that are found in modern desktop audio production applications.

How do I play a sound or music file in Windows?, To play an audio file, either click File and select Open and browse to the location of the file or drag the file to the RealPlayer window. You can also double-click the file to start playing the file immediately. However, if the audio file is associated with a different program, it may not open in RealPlayer. Are you trying to stream your PS4 to your computer / pc and when you are you using PS4 remote play there is no audio??? Alot of times when you plug your ps4 controller into your pc via a usb cord

Easy with Jquery

// set audio tags with no preload

<audio class="my_audio" controls preload="none">
    <source src="audio/my_song.mp3" type="audio/mpeg">
    <source src="audio/my_song.ogg" type="audio/ogg">
</audio>

// add jquery to load

$(".my_audio").trigger('load');

// write methods for playing and stopping

function play_audio(task) {
      if(task == 'play'){
           $(".my_audio").trigger('play');
      }
      if(task == 'stop'){
           $(".my_audio").trigger('pause');
           $(".my_audio").prop("currentTime",0);
      }
 }

// decide how to control audio

<button onclick="play_audio('play')">PLAY</button>
<button onclick="play_audio('stop')">STOP</button>

EDIT

To address @stomy's question, here is how you would use this approach to play a playlist:

Set your songs in an object:

playlist = {
    'song_1' : 'audio/splat.mp3',
    'song_2' : 'audio/saw.mp3',
    'song_3' : 'audio/marbles.mp3',
    'song_4' : 'audio/seagulls.mp3',
    'song_5' : 'audio/plane.mp3'
}

Use the trigger and play functions as before:

$(".my_audio").trigger('load');

function play_audio(task) {
      if(task == 'play'){
           $(".my_audio").trigger('play');
      }
      if(task == 'stop'){
           $(".my_audio").trigger('pause');
           $(".my_audio").prop("currentTime",0);
      }
 }

Load the first song dynamically:

keys = Object.keys(playlist);
$('.my_audio').append("<source id='sound_src' src=" + playlist[keys[0]] + " type='audio/mpeg'>");

Reset the audio source to the next song in the playlist, when the current song ends:

count = 0; 
$('.my_audio').on('ended', function() { 
   count++;  
   $("#sound_src").attr("src", playlist[keys[count]])[0];
   $(".my_audio").trigger('load');
   play_audio('play');
});

See here for an example of this code in action.

How To Play Audio in PowerPoint, In this article you will find the range of options to play audio in PowerPoint . Learn how to change volume, play in specific slides, trigger audio etc. This article is� Note: Computer sound cannot be shared while multiple screens are being shared. Instructions. To share computer audio such as YouTube, Pandora, etc. during screen sharing, click on Share Computer Sound. Screen Sharing with Computer Sound While Connected To Meeting Audio via Phone

Java Tip 24: How to play audio in applications, Playing audio clips in applets is quite simple and involves the following steps: Create an AudioClip object; Load .au sound file into AudioClip; Play sounds once or�

Sampling Tutorial 6: Record and Play Audio Files, Playing from memory vs. playing from disk You have already seen how to store sound in memory - in a buffer~ - by recording into it directly or by reading in a p.

MediaPlayer overview, You cannot play sound files in the conversation audio during a call. The basics. The following classes are used to play sound and video in the�

Comments
  • Since it's html5, there's <audio>. That element will have appropriate JS hooks for 'play', 'pause', etc...
  • @Marc Well could you provide some information on what those hooks are?
  • I bet you're wondering where the documentation is for all of the audio methods: stackoverflow.com/questions/4589451/…
  • This method works for playing mp3 but not for wav files. Is there some way to play wav files?
  • @user3293156 This method supports the same formats as HTML5’s <audio>. Wikipedia has a audio format compatibility table. new Audio() can play WAV files in all browsers except Internet Explorer.
  • @RoryO'Kane so everyone can play microsoft's audio format except microsoft? lol
  • Couple caveats: (1) Firefox won't play mp3. You'll need an ogg file instead. (2) Safari/iOS won't play if you're serving the content over https. You'll need to have a valid certificate.
  • Notice that from april 2018 on Chrome will not play audio files unless the user has at least clicked once in the document. See here.
  • Generally when you give a link on this website, you provide some info from the link in your post. It is discouraged to just post a simple link and say "click this."
  • Sorry, I figured the site would explain itself. I'll edit it then
  • This plays all audio tags with class = "my_audio" at the same time. How do you play one after another (in a play list)?
  • @stomy did you post this as it's own question on StackOverflow? If you do let me know. I could help you with that. otherwise, I'll offer this -- you could (after you start playing a song) listen for the ended event to be emitted and start the next audio file in the list (presumably you are keeping track either in the DOM, JS memory, etc). developer.mozilla.org/en-US/docs/Web/Events/ended
  • @stomy please check the EDIT in my answer to create a playlist that plays songs consecutively.
  • Did you mean to use </a> or </audio>. Just curious.