How to make Audio events alert when media stops for buffering - javascript

I have Audio file playing in jQuery. sometimes when the download speed is slower than the playing speed, audio can stop. I want to get a notification when this situation is happening and so for example the music stops and I get the console error every minute, then once the buffer is loaded enough to play the song then it stops sending the notification. In reality there will be a loading icon that appears and disapears. So i need both inititing event and stopping event.
This is the events I tried to have but I do not get the right results.
the list of events are here: link
$(audio).on("canplay, playing, ", function () {
console.log("stop Buffering");
});
$(audio).on("buffered", function () {
console.log("buffering");
})

buffered is a property of audio element, not an event. You should be listening to waiting event.
The waiting event occurs when the video stops because it needs to
buffer the next frame. This event can also be used on
elements, but it is mostly used for videos.
w3schools.com/tags/av_event_waiting.asp

Related

Video element pause while phone call

I've got an issue with my app and I could reproduce it on this sample: https://webrtc.github.io/samples/src/content/getusermedia/record/
During a phone call, if I open a tab with a video with audio element, the video automatically pause (and can see a pause event on the element)
This does not happen if the video element was loaded and played before the phone call start, or if the video has no audio.
Is this a normal behaviour, and can it be tracked (by an event other than the pause one) ? Is there any way to know there is a phone call ongoing, or any way to restart the video during the phone call (element.play() works, but a pause event immediately happens) ?
To reproduce:
On a mobile (iOS or Android)
Start a phone call
Open https://webrtc.github.io/samples/src/content/getusermedia/record/
The video is paused
If you manually launch element.play(), you can see the frame in the video updating, and then pause again
The record works correctly
what (likely) happens is that the video element (which needs to synchronize audio and video) is waiting for audio data but doesn't get any from the underlying layers when a phone call is active.
A possible workaround would be to change how you display audio and video, each in their own elements with MediaStream as srcObject that only have the respective audio and video track of the original stream.
Can you file a specification issue on https://github.com/w3c/mediacapture-main/ please? This is a rather interesting case that warrants some discussion.

playing audio in event listener still gives error

I have this in the window load listener function.
var audio = new Audio();
audio.src = "assets/silence.mp3";
audio.load();
document.getElementById("body").addEventListener(
'touchstart',
function(evt){ audio.play(); audio = 0; },
{capture:false,once:true,passive:true}
);
In chrome on android (with remote debugging open), I touch the screen and it (correctly) triggers trying to play the audio. However, it fails and logs this to the console:
(index):65 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.
How else am I supposed to trigger audio in a way that ensures the user initiates it, if not through a user interaction event listener?
aha. turns out, touchstart doesn't count as a "user interaction" from the perspective of the mechanisms preventing auto-play.
solution: swap it with click.
one thing I'm still frustrated with:
I'm using this specific snippet as a means of very quickly allowing audio to be played on the page so that the underlying html5 game can play audio without worrying about it being blocked by the browser.
however- because it needs to wait for a full "click" event (as opposed to a "touchstart" event), any audio that should be triggered on "touchstart" is going to be passed over (for the first time it would otherwise be triggered).
it seems like 1. there's no way to get around this to use it how I'd like, and 2. if you're able to just listen to the whole document for a "click" anyways, it's not actually doing anything from stopping unwanted audio.
If anyone can come up with a better solution, I'll swap the "correct answer" to that.
I had the same problem when I was making a web piano. The browser charged the piano on load, so you could press(actually touch the screen) the piano keys before you click something. What I did was to add a starting screen with a button to start the game and this solved it because you had to click something before you touch the piano keys.

Chrome Extension Event Page w/ Audio Never Stops

I have Chrome Extension that consists of a popup and an Event page that runs in the background. The Event page is really simple: it registers an onMessage listener and when that listener is called, it creates an Audio object with a remote .mp3 file as the source and plays it. When the Event page gets another message and audio is playing, it pauses the audio and stops the play back.
var audio;
chrome.runtime.onMessage.addListener( function(message, sender, sendResponse) {
if (audio && !audio.paused) {
audio.pause();
audio.src = '';
audio.load();
audio = null;
sendResponse({});
} else {
audio = new Audio("http://path/to/audio/file.mp3");
audio.play();
sendResponse({});
}
});
The popup jump simply sends a message to the Event page to start playing the audio. The audio plays fine and stops playing when the Event page receives the second message, but the issue is that when the Event page receives the second message, pauses, and sets the audio source to an empty String, the Event page never goes away. It will persist in the background like a Background page and never goes into it's inactive state.
I tried creating the audio var like this to see if using the Audio constructor was causing the issue:
audio = document.createElement('audio');
But that did not behave any different. I then tried setting the preload attribute to none instead of auto to no avail. I am wondering if Chrome is opening a socket to load the mp3 file and is not closing it properly, so the Event page still thinks that it is active because of the open socket, but that is just speculation at this point...
Is there something else that I need to do to "unload" the audio so that the Event page will go into an inactive state when audio is not playing?
After upgrading Chrome to version 36, the Event page goes inactive after the Audio element is unloaded.

What event to look for...when changing currentTime of video element

When i change video element current time to a point where it hasn't buffered the video stops playing. And i want to know when it starts playing as soon as the first frame.
I have tried canplay and playing without success.
Tho i did get it to work by using play and pause.
So when i pause it canplay event works and then i listen for it and which tells me that at new position at which i paused now can be played. Then i just play it however. Is it, should be, possible to do it without pausing. Why there is no readystatechange as in ajax. If video is paused,stopped,waiting for buffering(as in my case) fire an event.
This list of all media element events should be helpful:
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#event-definitions
The waiting and playing events should be relevant if you're just playing and the video pauses by itself. waiting fires when the video is forced to pause because it's buffering data from the network, and playing fires when it's ready to resume. It's worth noting the difference between play and playing. play fires when it's "trying" to play, i.e. .paused === false. But playing means that it's actually playing (although there are some weird cases where that's not strictly true, as noted in the linked document).
However, if you're seeking, you could use the seeking and seeked events. What I would do is keep a listener on all four events as well as canplay, and check the .readyState property in there.

Akamai Media Player: Get event callback when it's ready to play

I'm using the Akamai Media Player to stream video for an event. Normally the video stream starts the the "LIVE" position, but I would like mine to default to an earlier point in the video stream so that when users come to my page they always see something cool to start instead of the empty event grounds when the event isn't actively happening.
I'm trying to find a way to receive a callback when the video player is ready to go so I can tell it to seek back to the right number of seconds. The documentation says there is a 'loadeddata' callback, but for the life of me I can't get it to work. I'm never receiving any notifications. I can just set up a javascript timeout for 1.5 seconds that will cause it to seek back to the right time, but then I create a race condition and if the flash player (akamai player) hasn't properly loaded at that point it still won't work.
Documentation for the Media Player: http://videotest.esmas.com.mx/avp/userGuide/AkamaiPlayer_UserGuide.pdf
Any suggestions?

Categories

Resources