I need to add mp3-file audio track to RtcPeerConnection.
This work fine, local computer loads mp3-file, converts it to blob,
creates Audio() object, a stream, sends it track[0], a peer listens it.
Also I need to capture onended
event of the added track, but it is not fired.
However, the same event of the created Audio is captured normally – see comments in my code.
The question is – why newAudio onended
is captured and newTrackToAdd
‘s is not ?
My code is:
try {
// blobURL - is the data of loaded mp3-file
let newAudio = new Audio(blobURL); // CREATED OK
newAudio.autoplay = true;
newAudio.addEventListener("play", (e) => {
DisplayEventMessage("play: ", "NewAudio"); // THIS EVENT IS FIRED - OK
});
newAudio.addEventListener("ended", (e) => {
DisplayEventMessage("ended: ", "NewAudio"); // THIS EVENT IS FIRED - OK
// IT IS FIRED !!!
});
// create a stream to pass mp3-file
const ctxAudio = new (window.AudioContext || window.webkitAudioContext)();
const streamDest = ctxAudio.createMediaStreamDestination();
const streamSource = ctxAudio.createMediaElementSource(newAudio);
streamSource.connect(streamDest);
const newStream = streamDest.stream;
console.log("--- newStream ---");
console.log(newStream);
const addAudioTrack = true;
allTracks = newStream.getAudioTracks();
………
// get a track to add
let newTrackToAdd = allTracks[0];
newTrackToAdd.onended = function(e) {
alert("onended!"); // IT IS NOT FIRED !!! WHY ???
DisplayEventMessage("onended: ", "newTrackToAdd");
};
// add the track to webrtc - it works, a peer listens the mp3-file
rtc_addExtraTrack(curWebRtc, newTrackToAdd);