I'm working on a simple mp3 player application in Flash and am trying to get a time counter implemented that will keep track of how much time has elapsed from the currently playing track. I came across this code snippet online:
var music:Sound = new Sound();
music.loadSound("audio.mp3", true);
var minutes:Number = 0;
var seconds:Number = 0;
this.onEnterFrame = function() {
minutes = Math.floor(music.position / 1000 / 60);
seconds = Math.floor(music.position / 1000) % 60;
output.text = minutes + ":" + seconds;
}
However it seems that the position property is no longer part of the Sound class. I'm new to ActionScript, has this property been moved elsewhere? Or does someone have any thoughts on how to implement a time counter for mp3 progress?
The play method in Sound returns a SoundChannel which has the position property you're looking for.
Related
I extensively use the Audio and PositionalAudio part of ThreeJS and it seems to me that the play/pause function can not work correctly for a looped audio.
In the ThreeJS Audio source we can read:
this._pausedAt += Math.max( this.context.currentTime - this._startedAt, 0 ) * this.playbackRate;
If I am understand correctly, _pausedAt is thus somehow the elapsed duration of play in the audio file which is fine and totally usable (in particular for a straightforward playing with no loop). But when executing the play() function I see:
source.start( this._startedAt, this._pausedAt + this.offset, this.duration );
And in the MDN documentation of AudioBufferSourceNode.start():
offsets past the end of the audio which will be played (based on the audio
buffer's duration and/or the loopEnd property) are silently clamped to the
maximum value allowed.
That means that once the file has been read at least once until the end (with or without pauses), the next play() (typically after a pause()) will start at the end of the file (duration and/or loopEnd as mentioned) instead of restarting from the correct position where it was paused – somewhere in between loopStart and loopEnd.
Am I correct on this understanding?
If so, I see no better option than doing something like the code below to correct the position of the playhead (or _pausedAt):
if (sound.getLoop() === true) {
var looping = (sound.loopEnd === 0) ? sound.source.buffer.duration : sound.loopEnd
var loopDur = looping - sound.loopStart
_pausedAt = (_pausedAt - sound.loopStart) % loopDur + sound.loopStart;
}
Does any of this make sense?
Thank you very much,
Benjamin
Answered on ThreeJS discourse: https://discourse.threejs.org/t/pause-for-looped-audio/16894
Fixed here: https://github.com/mrdoob/three.js/pull/19079
What is your other way of contacting Benjamin? I can not post on ThreeJS forum.
I'm currently reading data from 7 sensors from bluetooth and displaying them with 7 lines in the chart. I managed to get the app working, for each data coming in, I implement the addEntry function below to update the chart and move the window along the x-axis to show the most updated data.
However, data transmission is at 100Hz, and the app will run for more than an hour, I found that the app freeze and might not be able to deal with this sometimes after running the app for a period of time, say 20 minutes. Given the large amount of data to deal with, I would like to just keep data for 10-15 seconds in the chart(i.e. even I scroll back to the beginning of the chart, it will just show 10-15s data). After some research, I just found the way
mChart.getData().notifyDataChanged(); to update data, which won't allow me to just display a selected part of the arraylist. Could any advice?
Thanks in advance.
private void addEntry(){
Log.d(TAG, "add Entry called");
LineDataSet[] sensorSet = new LineDataSet[7];;
if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0){
for (int k = 0; k<7; k++) {
sensorSet[k] = (LineDataSet) mChart.getData().getDataSetByIndex(k);
sensorSet[k].setValues(SensorData.getSensor()[k]);
}
mChart.getData().notifyDataChanged();
Log.d(TAG, "notified");
mChart.notifyDataSetChanged();
mChart.setVisibleXRange(0,5000);
int temp = mChart.getData().getEntryCount();
Log.d(TAG, "Entry count: " + mChart.getData().getEntryCount());
mChart.moveViewToX(mChart.getLineData().getXMax()-1000);
}else{
for (int k = 0; k<7; k++) {
sensorSet[k] = createSet(k);
}
LineData data = new LineData(sensorSet);
mChart.setData(data);
}
}
I'm making scene with a radio-like object, and I'd like to make it play a random audio file from my assets when is clicked. This question help me a lot to understand how to add the audio files Play sound on click in A-Frame
but I can't figure out the random aspect.
Thank you very much.
You can have an array of sound elements
// use ID's - grab by document.getElementById(audioIds[num])
var audioIds = ['one', 'two', 'three']
// use a class - grab by audioEls[num]
var audioEls = document.getElementsByClassName('sharedClassName')
and use a simple random to select an item
// get a random number between 0 and the number of the elements
let lastIndex = audioEls.length - 1 // arrays begin at 0.
var audioEl = audioEls[Math.round(Math.random() * lastIndex)]
Then on click stop any playing sound, and grab a new one:
this.el.addEventListener('click', () => {
if (!playing) {
audioEl = audio[Math.round(Math.random() * (audio.length - 1))]
audioEl.play();
} else {
audioEl.pause();
audioEl.currentTime = 0;
}
playing = !playing;
});
You can check it out in my fiddle.
Is it possible to get the mouse double click speed value from the OS with node ?
I try to get it to apply it for my application.
I guess it is not possible to get the time difference from the OS If you want only the time difference use some javascript logic to get the time difference my recommedation
var lastClickTime = 0;
$("#testLink").click(function() {
var d = new Date();
lastClickTime= d.getTime();
console.log("last time clicked " + lastClickTime );
});
store the values and do the functionality according to that
hope it will help
Followed a few tutorials and I'm having no luck, I'm trying to add a simple .WAV sound effect into XNA using free samples, I have named the sound as:
SoundEffect hit1;
SoundEffect hit2;
And then loaded the content with:
hit1= content.load<SoundEffect>("hit1")
But when it comes to adding the play to a button press and I go to test it there's no sound at all even no errors or nothing the game loads and is playable but any sound effects are not working.
** // Sounds
SoundEffect hit1;
SoundEffect hit2;
This is my variable names:
// Sounds
hit1 = Content.Load<SoundEffect>("hit1");
hit2 = Content.Load<SoundEffect>("hit2");
This is how I'm loading them in the loadcontent method:
//If Keyboard Key W is Pressed or Buttong Y
if (keys1.IsKeyDown(Keys.W) && oldKeys1.IsKeyUp(Keys.W)
|| (oldpad1.Buttons.Y == ButtonState.Released) && (pad1.Buttons.Y == ButtonState.Pressed))
{
secondsPassed = 0;
// IF The Target Image is a Gnome
if (targets[0] == sprites[0])
{
//They whacked a gnome
GnomeHits = GnomeHits + 1;
runNum = 0;
secondsPassed = 0;
hit1.Play();
}
else
{
//They whacked a troll
scoreNum = scoreNum + 1;
runNum = runNum + 1;
GnomeHits = 0;
secondsPassed = 0;
hit2.Play();
}
SetUpSpriteLoop();
And this is one of the control buttons I'm trying to assign sound too
when I hit F5 and run the game when I hit the key or button no sound at all.
I had a similar problem with Monogame (built from XNA's ashes) on a Windows 10 machine. I fixed it by reinstalling DirectX, and everything just worked.
My first recommendation would be to make sure you are updating the keyboard presses. I only say that as I can't see you doing that in your code. If you do not do this then the key states will not update and therefore you will never enter the if statement.
Once you have ensured you are entering the if statement, I would play around with the volume of the sound effect to make sure it is actually audible. I may be a bit off base there however if I am I suggest following this tutorial rbwhitaker is a great resource for XNA.