HTML5 Audio API stop a sound triggered with noteGrainOn - html5-audio

Is there a way to stop (or forever pause) a sound played with ..
audioSource.noteGrainOn(when, starthere, duration_sound);
..before the "duration_sound" variable stops it? Is it via NoteOff, but how is it used?

Yes. (note that you should use .start() - noteOn() and noteGrainOn() are deprecated.)
Just call
audioSource.stop( 0 );
to stop immediately, or you can schedule a stop before the sound is finished playing by calling
audioSource.stop( whentostop );

Related

Web speech API stops listening after some time passes without input

I'm using the web speech API but once a bit of time passes by (a minute or 2) without any vocal input, it stops listening entirely. I know this because I have it log its parsed text to the console, however, it stops doing this when I do not talk for a minute or two.
Is there any way to fix this?
You can listen to the end event and then restart the recognition on the SpeechRecognition object.
You should use a boolean flag for deciding (in the onend event handler), when to restart the recognition (and when not to restart).
You could use the other recognition-related events for this.
E.g. Chrome triggers the following event handlers when recognition is started:
1. onstart
2. onaudiostart
(only if sound / speech is detected)
3. onsoundstart
4. onspeechstart
If no sound speech is detected, only the first 2 will be triggered, and then, after some timeout, the corresponding end events (in reverse order).
A simple solution for this could probably be to listen for the end event and restart the recognition
recognition.addEventListener('end', recognition.start);
recognition.addEventListener('end', () => recognition.start()) works but Chrome browser seems to obstrruct the continuity by generating a pop-up to Allow or Block the microphone every 5-7 seconds.

Getting "Time Elapsed" of a playing video from the YouTube API in ActionScript 3.0

I am building a YouTube video player in Flash / ActionScript 3.0, using their API.
However I am struggling to find a simple way of getting a play progress indicator. All I am looking for is a numerical display that shows time elapsed / total duration. It doesnt need to be a bar or anything.
After going through the YouTube API, I cant see any kind of event or method from which to get this kind of information.
Without any kind of event the best thing I can think of doing is creating a timer to mimick the time progress, pausing the timer when the video buffer event is triggered, but this seems a bit hacky and wrong.
What is the best way of getting the time elapsed data from the YouTube API?
Yes in the Youtube API there are such informations:
player.getDuration():Number
Returns the duration in seconds of the currently playing video. Note that getDuration() will return 0 until the video's metadata is loaded, which normally happens just after the video starts playing.
If the currently playing video is a live event, the getDuration() function will return the elapsed time since the live video stream began. Specifically, this is the amount of time that the video has streamed without being reset or interrupted. In addition, this duration is commonly longer than the actual event time since streaming may begin before the event's start time.
player.getCurrentTime():Number
Returns the elapsed time in seconds since the video started playing.

AS3: Call external function periodically depending on played video stream

I implemented a flex application to play an incoming video stream from a Red5 Media Server.
private function playStream(streamName:String, offset:int):void {
stream = new NetStream(connection);
stream.play(streamName + ".flv", offset);
var streamVideo:Video = new Video();
streamVideo.attachNetStream(stream);
display.addChild(streamVideo); }
The playStream method plays a given stream from the position which is defined by offset parameter. Now I want to update my page content depending on the played video stream. Or more specifically I want to call an actionscript method that updates the content, after each minute in the video. Should I use Timer for that reason?
Best regards
Yes, you will need to user a Timer object. But don't use the Timer to determine where the user is at in playback of the video. You should use the time property of the NetStream instead.
You should also add an event listener for the NetStatusEvent in your playStream() method. In particular, you want to inspect the info property of this event (technically it's the info.code property). This has several useful messages that you will want to use to know: when the video playback starts/stops/pauses, when the user performs a seek, and so on. This way you can manage your Timer and update the UI efficiently when the user interacts w/the video player.
Some of the relevant codes on the NetStatusEvent are below. But inspect the full list, you might find others that will help you.
NetStream.Pause.Notify (the user paused playback, start/stop the timer here as appropriate)
NetStream.Play.Start (playback started, start the timer)
NetStream.Play.Stop (playback stopped, stop the timer)
NetStream.Seek.Notify (user seeked to a new point, update the UI)

Saving flash game state to server after window.onbeforeunload when swf is cross-domain

My Facebook app is a flash game. I want the game swf to save its latest state to the server when the window unloads. Since I embed the swf with swfobject, I use its embed handler to add a onbeforeunload listener to window:
function embedHandler(event)
{
shell=event.ref;
window.onbeforeunload=function(event)
{
shell.message("save", null);
//delay the unloading a bit so flash has time to contact server
var now = new Date().getTime();
var later=now+50;
while (now < later)
{
now = new Date().getTime();
}
}
}
Here's the problem. This works every time when the swf is loaded directly from the app (a rails app). It never works when the swf is loaded from Amazon.
All the cross-domain issues are worked out between the swf and the app--the rails app accepts calls from Amazon swf, and the Amazon swf loads data from the rails app.
ExternalInterface also works for both outgoing and ingoing calls. But I suspect this is nonetheless a browser security issue, since the inward-going ExternalInterface call only fails when:
it is called from inside the window.onbeforeunload handler
the swf originates from Amazon.
What is the problem? How does one unobtrusively save game state when the game is from a CDN and the save is triggered by onbeforeunload in Javascript? Or is there a better way to accomplish this same thing?
Testing in Firefox.
ExternalInterface also works for both outgoing and ingoing calls. But
I suspect this is nonetheless a browser security issue, since the
inward-going ExternalInterface call only fails when:
it is called from inside the window.onbeforeunload handler
the swf originates from Amazon.
From the sounds of it you worked out all the security issues.
It is more likely a lack of understanding on your part on what is going on behind the scene when onbeforeunload is triggered.
This is a function that will not wait for your "game.swf" to finish the call back via ExternalInterface.This is why you added a stalling mechanism to delay that process. However, I will assume here that this works from the rails app because that is a local server and you are not subject to the lag monster.
Now you might be thinking well I put in a delay it should work. Well that delay is on 50 milliseconds. Try increasing to to 5000(5 seconds) and you should see it start to work on the cdn.
The saving of data should be controlled via the flash app and not triggered by an outside source.
In the game itself you should have milestones that should trigger a save event.
In closing I do want to add that is by far the worst method you could use to save information up to a server. onbeforeunload is unreliable and is subject to cross browser issues let alone putting a lag loop in the JavaScript is just a bad idea and in the end just annoy your users to the point that they won't return.

Actionscript 3 NetStream immediately paused does not get metadata

I am writing a basic video player in Flash CS5 and Actionscript 3. For this basic player, I attach my NetStream to my NetConnection, then call the stream's .play() method to begin loading. Although I want the metadata and for the stream to begin buffering, I do not wish to start playing right away, so I immediately call the stream's .pause() method. Unfortunately, when I pause immediately, my stream's client's onMetaData event is not always called, so I don't necessarily get the total playtime of the loaded video.
As a workaround, I put the call to the "pause" method inside the onMetaData listener, but sometimes my video will have played a bit before receiving it's metadata, and will therefore continue to play until it does.
Is there a good way to stop my stream from playing, and still get my video metadata?
Okay, here's a neat little way of thinking about this differently... Do not attach your video object to your stream object right away. Start your stream playing while showing a "please wait" visual WITHOUT your video object being displayed. In your onMetaData listener, see if you have stored a duration previously. If not, assume this is the first call to onMetaData, store the duration, pause playback, seek the stream to 0, THEN attach the video object.
The user will see a "please wait" for just a sec, then the video will appear, paused & ready to be played with it's duration times as expected. The user will be completely unaware that the stream played forward a bit while they were waiting.
You should call pause when the NetStatusEvent.STATUS event NetStream.Play.Start is fired.
Update:
For very short streams (e.g. buffer > duration) NetStream.Play.Start is likely to get fired just before the onMetaData callback.
Before pausing on NetStream.Play.Start, check if metaData has been provided, if not don't pause straight but await onMetaData to pause (just set a flag, e.g pauseOnMetaData = true).