Play mp3 on body load call.

Joined
Dec 10, 2024
Messages
1
Reaction score
0
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 side that way they
dont need to click a button. I plan on removing the 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:playSegment(0.0, 20.5);">Play1</a>
<a href="javascript:playSegment(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.
 
Last edited:
Joined
Jul 4, 2023
Messages
539
Reaction score
70
It happen because:
How to make audio play on body onload?

For this code
HTML:
<button id="play-button-1">Play 1</button>
<button id="play-button-2">Play 2</button>

<audio controls autoplay>
  <source src="https://www.bensound.com/bensound-music/bensound-happiness.mp3" type="audio/mp3">
</audio>

<script>
  const audio_1 = new Audio('https://www.bensound.com/bensound-music/bensound-tenderness.mp3'),
        audio_2 = new Audio('https://www.bensound.com/bensound-music/bensound-creativeminds.mp3');

  const button_1 = document.querySelector('#play-button-1');
  button_1.addEventListener('click', () => {
    (audio_1.paused) ? audio_1.play() : audio_1.pause();
  });
  const button_2 = document.querySelector('#play-button-2');
  button_2.addEventListener('click', () => {
    (audio_2.paused) ? audio_2.play() : audio_2.pause();
  });

  window.onload = () => {
    try {
      //button_1.click();
      audio_1.play()
    } catch (err) { ; }
  }
</script>
1733964206460.png
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top