This site requires JavaScript, please enable it in your browser!
Greenfoot back
nooby123
nooby123 wrote ...

2013/2/10

Audio length?

nooby123 nooby123

2013/2/10

#
Is there any way to measure or get the length of a selected sound?
moobe moobe

2013/2/10

#
Tell me what you wanna do, 'cause I think that there will be other ways to solve your problem ;)
nooby123 nooby123

2013/2/10

#
I want to measure the length of a sound file.
danpost danpost

2013/2/10

#
If you want to measure how long the sound plays for, add an instance field to the class that plays the sound to hold the system time at the moment to sound starts. Then continuously check to see if the sound is still playing, and when it finally stops, substract the starting time from the time at which it stopped. That will be how long it played.
// add the instance field
private long soundStartTime;
// also, you should have
GreenfootSound sound = new GreenfootSound("nameOfFile.wav");

// the rest is in the 'act' method

// to begin the sound (press any key)
if (soundStartTime == 0 && Greenfoot.getKey() != null)
{
    sound.play();
    soundStartTime = System.currentTimeMillis();
}
// to check for sound stopping
if (soundStartTime != 0 && !sound.isPlaying())
{
    long elapsedTime = System.currentTimeMillis()-soundStartTime;
    System.out.println("The sound took "+(elapsedTime/1000)+" seconds to play.");
    soundStartTime = 0;
}
nooby123 nooby123

2013/2/10

#
I want to get a variable that holds the length of the audio before the actual sound plays to print it.
danpost danpost

2013/2/10

#
The play length is not something that can change. If you need to keep a reference of its length, just add it directly as an instance field. Let us say you ran the code above and the output was 'The sound took 4.263 seconds to play.'
// just add the instance field to hold the length in milliseconds
private long soundLength = 4263;
EDIT: time the sound multiple times and take the average maximum time (do not include the first time, the best time, or the worst time in getting the average maximum time).
nooby123 nooby123

2013/2/11

#
I have a music player that plays an audio, but I want to show how far the audio is through compared to its full length.
danpost danpost

2013/2/12

#
What you need is a status bar that displays the value of one hundred times the amount of time played so far divided by the total amount of time to play the whole audio; this calculation will produce a percentage (using values of 0 at the start through to one hundred at the end). You could create your own; or there are various support classes on-site that supply status/health/counter/timer/progress. In fact, click on my icon, and I believe that under 'Some scenario by danpost' you will find my 'Progress Bar/Health Bar Support Class'. I have another scenario that has several sub-classes that can be implemented with it (Bar Subclasses). Check out my 'Support Classes by danpost' collection
You need to login to post a reply.