Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Play mp3 on body load

Bobby2025b

New Coder
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.😀

<body onload="test1()">


<button onclick="test1()">test1</button>



<script type="text/javascript">
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.

>>> before txt3.value="apostles-authority.mp3"


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;





}
</script>

<div id="myDIV" class="hide">

<audio id="sample" src="" controls preload onfocusout="myFunction1()" ></audio>

</div>
<a href="javascript😛laySegment(0.0, 20.5);">Play1</a>
<a href="javascript😛laySegment(660.55515);">Play1</a>





<script>
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();
}
</script>


I'm working I'll respond as soon as I can. Thank you God bless.
 
Hey @Bobby2025b,
Could you kindly edit your post putting code into code blocks, or inline code for single lines and referencing parts of your code.
I would normally do this for you and just give a reminder, but it seems you've mixed some JavaScript code with regular text, so it's not very clear.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom