DEV Community

Bobby Thomas
Bobby Thomas

Posted on

Play mp3 file on <body onload

Im trying to play sound onload body
//section 1
works only if i click
button on click test1()
my code works...but doesn't play the file once
body onload fucntion
call runs

in , function test1

trying to play sound

tried doesn't run onload body

i placed my explanation inside my code below
so you can see what I'm doing...I left out full source code including my php code to save time. The parts I've added explains each section what I'm trying to accomplish. I hope
It's easy. I know there got to be a way.
If you try to run code my explanation will cause errors. I should of put a /* to ignore My
Notes. Here we go... planning on making a Christain website playing church services and bible study. Thank you anyone who can help
Jesus loves you.😀

test1

function test1() {

if i use thr onload function call to test1
file wont play.

it only plays if i click the button on click test1()
l as you see my button above.

then it works. my goal is to run when page load
on load on the clients that way they
need to click a button.

section 2
var audio = document.getElementById('sample');

loads mp3 to play. i use this box to manually
change mp3 file it prepares the mp3 to play
since i need to change mp3 often i need to use the text box
as you see below txt3.value holds to file
im playing next i know i can use
html scr,="apostles-authority.mp3" to load in
the audio player. i removed it because i mentioned
ill do it using my php code manually.
i didn't include ,my php loads when my iframe loads .
there i read the php code.

section 3
. i use a file
below the file mp3 to play

txt3.value gets the file that my php reads in
using php open .pulls file
called apostles-authority.mp3 loads the name/ of the file manually
from a textbox inside myiframe1.textarea2
like you see below.
txt3.value = iframe1.textarea2.value

my php that reads the file playing next
into iframe1.textarea2.value.
reason i use php in the iframe. php lets
me use the open file code from the server.
that you see in section 3, the list of files
each file has @ infront. i use a split code in my php
code to put
i use index[0] gets first file copies the name
into iframe1.textarea2.value
later is used for my audio player

before.

&gt;&gt;&gt;&gt; before txt3.value="apostles-authority.mp3"
Enter fullscreen mode Exit fullscreen mode

after
txt3.value = iframe1.textarea2.value

my audio file player id="sample"
var audio = document.getElementById('sample');

now my file im playing next is ready
audio.src = "apostles-authority.mp3"; //This is a
audio.src = txt3.value; //

'

im not posting full code im trying to keep
it short.

var music = 'apostles-authority.mp3'

I'm not inluding my php code that reads file from
local serverside.

the php open my file of mp3

section 3
@apostles-authority.mp3
@apostles-authority1.mp3
@apostles-authority3.mp3
pulls index.[0]
apostles-authority.mp3 reads it from
iframe1.textarea2.value as below shows
txt3.value = iframe1.textarea2.value

note it plays when i click the button
all works well..but it don't play body onload
like below,
<body onload="test1()">

i also added 2 counter timers to delay
while file name is pulled from my php code .
2 timmer runs call function test1() to play file
but still wont
play.
i didn't include the 2 timmer here.

playSegment(660.55515);

audio.play();

return false;

}

Play1
Play1

var audio = document.getElementById('sample');
var segmentEnd;

audio.addEventListener('timeupdate', function (){

$
if (segmentEnd && audio.currentTime >= segmentEnd) {

pause.play();

alert("")

}
console.log(audio.currentTime);
txt1.value =audio.currentTime

window.onblur = function (){ document.title='NOT FOCUSED'
audio.pause()
txt2.value ="false"

document.getElementById('iframe2').contentWindo

document.getElementById('iframe1').contentWindow.location.reload();
txt1.value = iframe1.textarea1.value
txt3.value = iframe1.textarea2.value

}
window.onfocus = function (){

document.title='FOCUSED'
test1()
txt2.value ="true"
audio.play();
}

let aud = document.getElementById("sample");
aud.onended = function() {
// alert("The audio has ended");
txt1.value = iframe1.textarea1.value
txt3.value = iframe1.textarea2.value
//jump1
//alert("")
//txt1.value=0.0
if (txt6.value =="")
{
//txt1.value = iframe1.textarea1.value

}
else
{
txt1.value = txt6.value

test1()
txt6.value=""

document.getElementById('iframe2').contentWindow.location.reload();

document.getElementById('iframe1').contentWindow.location.reload();
}

playSegment(660.55515);

audio.play();

};

}, false);

function playSegment(startTime, endTime){
segmentEnd = endTime;
//audio.currentTime = startTime;
audio.currentTime = txt1.value ;

audio.play();
}

I'm working I'll respond as soon as I can. Thank you God bless.

Top comments (0)