I'am beginner in HTML5 and not even middle in JS, but I'am trying to figure out how to correctly use <audio> and smth else... so i've got this question... my html:
<i class='some_icon' onclick='play();'>
<audio src='some_mp3.mp3' id='player'></audio>
</i>
Now when I click on the icon I want some mp3 file play and it works because I wrote this js:
function play() {
var my = document.getElementById('player');
if (my.paused) {
my.play();
} else {
my.pause();
my.currentTime = 0
}
}
BUT, besides, I want to change my icon WHEN that mp3 file ENDS... how can I do that? Any help will be greatly appreciated :)
Here is the code to trigger some code when your audio ( or video ) ends:
var my = document.getElementById('player');
my.addEventListener("ended", function(e){
// here goes your code to change the icon
}, false);
I know this was answered/accepted yesterday but i just thought I'd share a full working bit of code for future users benefit. (the sound clip is only 4 secs long)
function play() {
var my = document.getElementById('player');
my.currentTime = 0;
/**this one loops***/
if ($('.btn').find('span i').hasClass('fa-stop')) {
$('.btn').find('span i').removeClass('fa-stop').addClass('fa-play-circle');
$('.texto').text("Play");
}
if (my.paused) {
my.play();
} else {
my.pause();
my.currentTime = 0;
}
my.addEventListener("ended", function(e) {
$('.btn').find('span i').removeClass('fa-play-circle').addClass('fa-stop');
$('.texto').text("Stop");
});
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Audio Icon Toggle</title>
</head>
<body>
<div id="music">
<h2>Audio Icon Toggle</h2>
<button type="button" class="btn btn-success" data-toggle="collapse" data-target="#player" onclick="play();"><span><i class="fa fa-play-circle" aria-hidden="true"></i> <span class="texto">Play</span></span>
<audio src="http://www.rachelgallen.com/monkey.mp3" id="player" class="collapse"></audio>
</button>
</div>
</body>
</html>
Related
I'm a new in this world.
I won't be questioning every step promised ^^' but, I am following a tutorial which is pretty good but the problem comes when the < video > tag part in JS, which is from 2014...so there are probably updates since then.
Well, I can't get the < video > working when I wish to press the "Play" button. I've done the same as him + the possible updates (such as: simple "()", type for source, etc). Nothing :/
However, if I add into HTML sheet, (< script > tag) it works....
HTML
<!DOCTYPE html>
<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>CANARIAS</title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1404.47">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="main.js">
</head>
</script>
<body>
<section id="video">
<video id="mivideo" width="720" loop>
<source src="/video/video-2012-04-05-14-22-32.mp4" type="video/mp4">
<source src="/video/video-2012-04-05-14-22-32.ogg" type="video/ogg">
<source src="/video/video-2012-04-05-14-22-32.webm" type="video/webm">
</video>
<nav>
<div id="botones">
<button type="button" id="reproducir">Play</button>
</div>
<div id="barra">
<div id="progreso"></div>
</div>
</nav>
</section>
</body>
</html>
JAVASCRIPT
var mivideo, reproducir, barra, progreso;
function comenzar() {
mivideo = document.getElementById("mivideo");
reproducir = document.getElementById("reproducir");
barra = document.getElementById("barra");
progreso = document.getElementById("progreso");
reproducir.addEventListener("click", clicando, false);
progreso.addEventListener("click", adelantando, false)
}
function clicando() {
if ((mivideo.paused==false) && (mivideo.ended==false)) {
mivideo.pause();
reproducir.innerHTML = "Play";
}
else {
mivideo.play();
reproducir.innerHTML = "Pause";
}
}
window.addEventListener("load", comenzar, false);
If you thinks that it work fine with internal Js but not with external one, Then I thinks that the problem is that you haven't linked your JavaScript file with you HTML one.
Check out the complete guide on how and where to correctly link a Js file in HTML...
I am making an offline application of a music website using JS and I am handling song queues using arrays. I added song blocks with skip buttons so users can choose which song to skip without spamming another skip button that only allows them to skip the first song of the queue. The problem is, splicing the array with the index of the selected song the users wanted to skip removes the rest of the songs after it.
Example array: ["songA","songB","songC","songD","songE"]
If the user choose to skip songC, the song queue will become ["songA","songB"] and the songC to songE will be removed, but in reality the expected outcome should be ["songA","songB","songD","songE"].
I have no idea what is wrong with the code and I have searched similar topics regarding this problem but still no results. I am still new to JS and my code's a bit rough but I'm pretty sure that's not the cause of the issue.
JS:
//function of skip buttons on song blocks
songPlaylist.addEventListener("DOMNodeInserted", function() {
iconList = document.querySelectorAll(".deleteIcon");
iconList.forEach(function(element) {
element.addEventListener('click', function() {
var nameOfSong = this.parentNode.childNodes[0].dataset.name;
var indexByName = songQueue.indexOf(nameOfSong);
var selectedBlock = this.parentNode;
if (songQueue.length == 1) {
songQueue.shift();
songPlaylist.removeChild(songPlaylist.firstChild);
var emptyMsgSpan = document.createElement("div");
emptyMsgSpan.className += "emptyMsgSpan";
var emptyMsg = document.createTextNode("Playlist is empty! How 'bout adding some music?");
emptyMsgSpan.appendChild(emptyMsg);
document.getElementById("playlistContainer").appendChild(emptyMsgSpan);
setTimeout(function() {
audioPlayer.src = "";
}, 1000);
} else {
songQueue.splice(indexByName,1); // <--- press first block output delete everything
// press any block after first deletes everything beneath it
songPlaylist.removeChild(selectedBlock);
for (var blockIndex = indexByName; blockIndex <= songQueue.length; blockIndex++) {
document.getElementById("playlistContainer").children[blockIndex].childNodes[1].dataset.index -= 1;
}
};
});
});
});
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="index.css">
<link href="https://fonts.googleapis.com/css?family=Bai+Jamjuree" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Jost&display=swap" rel="stylesheet">
<!--<div>Icons made by Roundicons from www.flaticon.com</div>-->
<title>Mue - Custom Playlist</title>
</head>
<body>
<div id="bg">
<div class="titleBar">
<button style="background: none; border: none; cursor: pointer;" onclick="location.reload(); return false;">
<span>MUE</span>
</button>
</div>
<div class="songNameDisplay">
<span id="nowPlaying"></span>
</div>
<div class="trackContainer">
<div class="btn play"></div>
<div class="audioTrack">
<div class="trackBtn"></div>
</div>
<div id="skipBtn">
<div class="triangle1"></div>
<div class="triangle2"></div>
<div class="rect"></div>
</div>
</div>
<button id="fileBtn" class="fileBtn"><span>Upload</span></button>
</div>
<audio id="audioPlayer" controls autoplay></audio>
<input type="file" id="file" />
<div id="playlistContainer"></div>
</body>
<script src="index.js"></script>
<script>
const bg = document.getElementById("bg");
window.addEventListener('scroll' , function() {
let offset= window.pageYOffset;
bg.style.backgroundPositionY = offset * 0.5 + "px";
});
</script>
</html>
Thanks in advance.
What do the numbers in the parenthesis mean?
For ex, audio.js(1,1)
.
Oh yeah, this code is to autoplay music in my webpage.
HTML CODE:
<head>
<title> Customized audio web page </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
These links are like the libraries for the plus, minus and speaker symbols.
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<body>
Volume control
<button onclick="volume_down()" style="font-size:24px;color:blue"><i class="fa fa-minus-square"></i></button>
<button onclick="volume_switch()" style="font-size:24px;color:blue"><i class="material-icons">volume_off/volume_up</i></button>
<button onclick="volume_up()" style="font-size:24px;color:blue"><i class="fa fa-plus-square"></i></button>
<audio id="bgm" controls autoplay hidden="hidden">
<source src="Z:\Pollution Levels\Eternal Pose.mp3"/>
</audio>
<script src="audio.js"></script>
</body>
</html>
This code is to increase or decrease the volume in steps of 10%.
JavaScript Code:
var aud = document.getElementById("bgm");
aud = {vol:0.5};
function volume_up() {
if(aud.vol=1.0)
{aud.vol = 1.0;}
else
{aud.vol += 0.1;}
}
function volume_down() {
if(aud.vol=0.0)
{aud.vol = 0.0;}
else
{aud.vol -= 0.1;}
}
Hey, if I store an object value, aud.vol in a variable, will the prev_vol be update when aud.vol changes (this is to restore the volume of music after unmuting).
function volume_switch() {
var prev_vol = aud.vol;
if(aud.vol>0.0)
{aud.vol -= aud.vol;}
else if(aud.vol=0.0)
{aud.vol = prev_vol;}
}
Line and character positions respectively.
Why doesn't image show on load, but shows on refresh?
My problem is like the one above, except with multiple video files. On Chrome, videos won't display unless after refresh. On Safari, they seem to display fine. Per the other answer, I'm assuming this has to do with files loading asynchronously and can be fixed by proper use of onload in my jquery, but I'm unfamiliar with how to best do that.
HTML
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="/hinukan/style.css">
</head>
<body>
<div id = "container">
<div id="box1" class = "box">
<div id="video1" class="video"> <video poster loop muted playsinline controls><source src=assets/portrait.mp4></video></div>
<div id="text1" class="text"><p>mytext</p></div>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="/hinukan/javascript.js"></script>
</html>
JAVASCRIPT
if ($(".video").css("max-width") == "850px" ){
$(".box").children(".text").click(function() {
$(this).toggleClass("ttoggle");
$(this).siblings(".video").toggleClass("vtoggle");
});
$('video').click(function(){
this[this.paused ? 'play' : 'pause']();
});
}
else {
$(".box").children(".video").click(function () {
if (!$(this).hasClass("vtoggle")) {
$(this).siblings(".text").toggleClass("ttoggle");
$(this).toggleClass("vtoggle");
}
});
$(".box").children(".text").click(function() {
if ($(this).hasClass("ttoggle")) {
$(this).siblings(".video").toggleClass("vtoggle");
$(this).toggleClass("ttoggle");
}
});
$(".video").hover(function () {
$(this).siblings(".text").addClass("tselect");
$(this).children("video")[0].play();
}, function () {
var el = $(this).children("video")[0];
el.pause();
$(this).siblings(".text").removeClass("tselect");
});
}
i want to create a simple chrome App which launches a window, 'window.html', which contains 2 buttons.
1/ #btn1 creates a new window, loading "video.html". A video player,
playing "file1.webm".
2/ #btn2 updtates the source of the video from "file1.webm" to
"file2.webm".
The first part is trivial :)
The second part is tricky.
Is it possible ?
You'll find my files below.
Thank you :)
<!DOCTYPE html>
<html >
<head>
<title>Chrome : Multiple Window</title>
<link href="./css/main.css" rel="stylesheet">
<script src="./js/jquery.min.js"></script>
<script src="./js/test.js"></script>
</head>
<body>
<button id="btn1" type="button" >Launch video Player</button>
<button id="btn2" type="button" >Update Video</button>
</body>
</html>
$(document).ready(function() {
$("#btn1").click(function(){
chrome.app.window.create('video_window.html', {"width":1280, "height": 720});
});
$("#btn2").click(function(){
$('#myvideo video source').attr('src', './video/avatar.webm');
});
});
<!DOCTYPE html>
<html>
<head>
<link href="./css/video.css" rel="stylesheet">
</head>
<body>
<div class="wrapper">
<video id="myvideo" autoplay loop>
<source src="./video/the_master.webm" type="video/webm">
</video>
</div>
</body>
</html>
chrome.app.window.create accepts a callback which will be invoked with the created window. You can store a reference to this window, and then either execute functions directly on it, or use window.postMessage to communicate with it.
var videoWindow = null;
$("#btn1").click(function() {
chrome.app.window.create('video_window.html', {
"width": 1280,
"height": 720
}, function(window) {
videoWindow = window.contentWindow;
});
});
$("#btn2").click(function() {
videoWindow.doSomething('./video/avatar.webm');
});
Another option, is to use the chrome runtime API to communicate:
chrome.runtime.sendMessage("do-stuff")
chrome.runtime.onMessage.addListener(function(e) {
// do stuff
})