Abhi Thakkar
Abhi Thakkar

Reputation: 171

How to merge two audio files into one and make it play after merge in javascript

I have one track which I want to add in background and one recorded audio. I want to merge both the audios and make it one, than play that merged audio. Can anyone help me out how to merge audios and than play it.

Upvotes: 2

Views: 2405

Answers (2)

Jawad Khan
Jawad Khan

Reputation: 341

You can't merge files at client side. If you only want to play both together you can just play them together.

<audio id="myAud">  
  <source src="sounds/1.mp3" type="audio/mpeg">
</audio>
<audio id="myAud2">  
  <source src="sounds/2.mp3" type="audio/mpeg">
</audio>

<script>


var audio1 = document.getElementById("myAud");
var audio2 = document.getElementById("myAud2");

audio1.play();
audio2.play();


</script>

Or if you choose to play one after another:

<script>


var audio1 = document.getElementById("myAud");
var audio2 = document.getElementById("myAud2");

audio1.play();
audio1.addEventListener('ended', function() {
    // first one complete play next..
audio2.play();
},false);

</script>

Upvotes: 1

digitalTrilunaire
digitalTrilunaire

Reputation: 105

If you always play these two files together, I suggest you to merge them with ffmpeg. Otherwise why not play the first file and at the end of this file play the other (How to detect an audio has finished playing in a web page?) ?

Upvotes: -1

Related Questions