Why sound cannot be played in android version 4.1? When in browser sound can be played very well.
i am trying to play the sound when the card match and when the card mismatch.
I have currently developed my game using HTML5, CSS and javascript.
Javascript:
function playAudio(sAudio) {
var audioElement = document.getElementById('audioEngine');
if(audioElement !== null) {
audioElement.src = sAudio;
audioElement.play();
}
}
function playAudio(sAudio) {
var audioElement = document.getElementById('audioEngine');
if(audioElement !== null) {
audioElement.src = sAudio;
audioElement.play();
}
}
function checkPattern() {
if (isMatchPattern()) {
$(".card-flipped").removeClass("card-flipped").addClass("card-removed");
if(document.webkitTransitionEnd){
$(".card-removed").bind("webkitTransitionEnd", removeTookCards);
}else{
playAudio("mp3/WinLevel.mp3");
removeTookCards();
}
} else {
$(".card-flipped").removeClass("card-flipped");
playAudio("mp3/WrongLose.mp3");
}
}
HTML5:
<audio id="audioEngine">
our browser does not support the audio element.
</audio>
Can anyone help me?
Related
I can not play audio on autoplay in the new version chrome br!
Help me guys
const myAudio = new Audio('transient.mp3');
myAudio.loop = true;
myAudio.addEventListener('ended', function () {
this.currentTime = 0;
this.play();
}, false);
myAudio.play();
Because the user will need to have interacted with the domain before music can play. See https://developers.google.com/web/updates/2017/09/autoplay-policy-changes
When I hover my video control bar in Chrome, Safari it shows up. But when I hover that in firefox, it's not showing. I don't know if my js code doesn't support firefox. But when I inspect it in firefox, the controls keep appearing and disappearing. Below is my code. Can anyone help me with this? Thank you.
HTML
<video poster="http://dummyimage.com/320x205/852285/fff" preload="auto">
<source type="video/mp4" src="http://www.w3schools.com/html/movie.mp4" />
</video>
JS
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/mediaelement/2.13.1/js/mediaelement.js'></script>
// Video Player
function videoPlayer() {
// Exit full screen when video is done playing
var video = document.getElementsByTagName("video")[0];
video.addEventListener("ended", function(e) {
video.webkitExitFullScreen()
});
var player = $('.video-player'),
controls = player.find('.vid-play-btn-wrap'),
wrapper = player.find('video'),
video = player.find('video').get(0),
isPlaying = false,
settings = {},
media = new MediaElement(video, settings),
$media = $(media);
$media.on('play', _playHandler);
$media.on('pause', _pauseHandler);
$media.on('ended', _endedHandler);
player.click(_togglePlayPause);
player.hover(_mouseOverHandler, _mouseOutHandler);
function _togglePlayPause() {
isPlaying ? media.pause() : media.play();
}
function _mouseOverHandler() {
if(!isPlaying) { return; }
// controls.fadeIn('fast');
}
function _mouseOutHandler() {
if(!isPlaying) { return; }
// controls.fadeOut('fast');
}
function _endedHandler() {
isPlaying = false;
video.load();
controls.show();
}
function _playHandler() {
isPlaying = true;
controls.hide();
}
function _pauseHandler() {
isPlaying = false;
}
$('video').hover(function toggleControls() {
if (this.hasAttribute("controls")) {
this.removeAttribute("controls")
} else {
this.setAttribute("controls", "controls")
}
});
}
you have this in your script:
player.hover(_mouseOverHandler, _mouseOutHandler);
So BOTH functions are executed on hover (which explains the appearing and disappearing). It might be better to create ONE function that contains both things you want in an if/else statement
The following script is playing a soundfile when i click on a img (onclick). How do i pause it by clicking on the same img? I´ve tried audio.pause(), but it won´t work.
function play(){
var audio = document.getElementById("myaudio");
audio.style.display="block";
audio.play();
}
<img src="Bilder/play2.png">
You should rename your function to audioHandler() for example which will handle whether to play or pause your audio.
Create a boolean to remember if your audio was playing or was on pause.
//initial status: audio is not playing
var status = false;
var audio = document.getElementById("myaudio");
function audioHandler(){
if(status == false || audio.paused){
audio.play();
status = true;
}else{
audio.pause();
status = false;
}
}
Check the media.paused property of your HTMLMediaElement
function play_pause(media) {
if (media.paused) {
media.play();
else {
media.pause();
}
}
Use this in the handler on the element you want to control the action
var elm = document.getElementById('play_button'),
audio = document.getElementById('myaudio');
elm.addEventListener('click', function (e) {
play_pause(audio);
});
I am facing a problem for playing sound in javascript. The alert box is displayed but the chat sound doesn't play. Following is the code of chat.js.
for (x in newMessages) {
if (newMessages[x] == true) {
if (chatboxFocus[x] == false) {
//FIXME: add toggle all or none policy, otherwise it looks funny
$('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink');
alert('before ');
var snd = new Audio("facebookchat.mp3");
snd.play();
alert('middle');
alert('after');
}
}
}
Try this:
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'facebookchat.mp3');
audioElement.load()
audioElement.addEventListener("load", function() {
audioElement.play();
}, true);
Reffered: How to use a javascript to autoplay a html5 audio tag
It might help, in your case.
this does not work for me! anyone having the same issue? my code seems straightforward but it only plays once I need to be able to play over and over. see as follows:
My Html:
<audio id="siren" src="sounds/400.ogg" preload="auto">
and my JS:
function play_siren() {
log("play sound");
if(document.getElementById('siren').paused == true){
document.getElementById('siren').play();
}
}
function pause_siren() {
try{
if(document.getElementById('siren').paused == false){
document.getElementById('siren').pause();
}
log("paused siren");
}catch(err){
log(err.message);
}
try{
if(document.getElementById('siren').currentTime > 0.0){
document.getElementById('siren').currentTime = 0.0;
}
log("reset siren");
}catch(err){
log(err.message);
}
}
it is started programmatically in the JS code as follows:
if(Obj[3].siren == true && isPlayingSiren == false){
play_siren();
isPlayingSiren = true;
}else if(Obj[3].siren == false && isPlayingSiren == true){
pause_siren();
isPlayingSiren = false;
}
I found this gentleman's code and his DOES seem to work: http://html5.komplett.cc/code/chap_video/js_audioPlayer_en.html
But setting "currentTime = 0" is not doing anything for me.
I can't figure it out. I tested with Chrome, Firefox and Safari. Firefox does not work at all so far Chrome seems to be the best for HTML5 but still I can only play once.
I even tried mp3 and ogg same behavior.
Please shed some light. Thank you!
here is full HTML:
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" media="all" href="js_audioPlayer.css">
<title>HTMLMediaElement example - Audio Player</title>
<script src="../js/audioPlayer.js"></script>
</head>
<body>
<fieldset>
<audio src="sounds/400.ogg" preload="auto"></audio>
<legend>Audio Player (400)</legend>
</fieldset>
<script>
function loadXMLDoc(){
_pause();
updateProgress(0);
playPause();
}
var loadXmlTimer = setInterval(loadXMLDoc, 2000);
</script>
</body>
</html>
and here is full javascript:
// global callback functions
var playPause, updateProgress, _play, _pause;
/* initialize audio player */
window.onload = function() {
// keep track of playback status
var AudioStatus = {
isPlaying : false
};
// define references to audio, pulldown menu, play-button, slider and time display
var audio = document.querySelector("AUDIO");
/* load track by menu-index */
var loadTrack = function(idx) {
audio.src = '../sounds/400.ogg';
audio.load();
};
/* callback to play or pause */
_play = function() {
audio.play();
log("play");
AudioStatus.isPlaying = true;
};
_pause = function() {
audio.pause();
log("pause");
AudioStatus.isPlaying = false;
};
playPause = function() {
if (audio.paused) {
_play();
}
else {
_pause();
}
};
/* callback to set or update playback position */
updateProgress = function(value) {
audio.currentTime = value;
log("updateProgress");
};
};
the timer plays the sounds programmatically on the expiration. this works as long it is private. I did not understand why it has to be private.
Does it pass thru the if statement?
You could use ontimeupdate for placing the value of document.getElementById('siren').currentTime in a variable.
If set to more than 0, then you could update it in an if conditionnal.
All of the previous solutions are unacceptable because they download the same sound file for each time the sound is played! Do this instead, for the sake of simplicity and efficiency:
var sound = document.getElementById("mySound")
if(window.chrome){
sound = sound.cloneNode()
}
sound.play()
This is how I fixed the problem with chrome specifically.