• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Why beep.wav sounds only once?

 
Ranch Hand
Posts: 98
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have a question regarding this code snippet:




My question is why the sound is played only once instead of twice?

Other way... how to make the audio file to sound two times in a row?

Thanks in advance!!

 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think the sound will played twice ?
 
Marshal
Posts: 25677
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that the sound would be played once, but for twice as long as if player.play() was only called once.

I could be wrong though, there might be more to it than that. However it's a testable hypothesis.
 
Alfonso Sanz
Ranch Hand
Posts: 98
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I expect for the sound todo.be played twice because player.play() is called two times. Ten... why is sounding once?

BR,

 
Paul Clapham
Marshal
Posts: 25677
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alfonso Sanz wrote:I expect for the sound todo.be played twice because player.play() is called two times.



And would you expect there to be a gap between the two sounds? If so, why? There's no code to produce a gap.
 
Alfonso Sanz
Ranch Hand
Posts: 98
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also added Thread.sleep() with a time greater Thanksgiving the Jeep durstion in between but only one os played.
 
Alfonso Sanz
Ranch Hand
Posts: 98
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also added Thread.sleep() with a time greater than the beep duration as parameter in between but only one os played.
 
Paul Clapham
Marshal
Posts: 25677
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:



 
Alfonso Sanz
Ranch Hand
Posts: 98
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give it a try.
Thanks!
 
Saloon Keeper
Posts: 12147
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably want to use setCycleCount().
 
Alfonso Sanz
Ranch Hand
Posts: 98
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just what I needed! Thanks you Stephan van Hulst!
 
Alfonso Sanz
Ranch Hand
Posts: 98
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more question:

Here is the second piece of code:



In spite of the delay, both sounds are being played in parallel. Why? How to make a real "gap"?

By the way... under Windows everything runs conveniently, but under RHEL,  when creating looks like randomly I get MediaException in init(). Enybody knows??

Thanks in advance.
 
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of
what about Thread.sleep(1000); // one second

That may not be the best way of getting a delay in JavaFX, though.
 
Alfonso Sanz
Ranch Hand
Posts: 98
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Instead of
what about Thread.sleep(1000); // one second

That may not be the best way of getting a delay in JavaFX, though.



Thread.sleep() has the same effect. Both sounds at a time.
 
Knute Snortum
Sheriff
Posts: 7108
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example that I think you can fit to your needs.  It's taken from the book Learning JavaFX 8 by APress.  I think it's fair use to reproduce it here.  If not, then I'll delete the post.  Please buy this book if you are serious about JavaFX programming.
 
Alfonso Sanz
Ranch Hand
Posts: 98
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great post and great book! Thanks Knute Snortum.

For the time being I am just invertigating how to play sounds and still not sure whethrt to use Clip.loop() or Mediaplayer.play(). It's not about JavaFX.

I also consider using "aplay" system call cause so far this approach is working best on Linux, where as I said above, two instanciations of MediaPlayer give sometimes MediaException.

In anycase, I'll consider to buy the book you recommend me. It looks quite interesting.

Thanks again.

 
Rancher
Posts: 387
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some really poor suggestions on this thread.

Don't run a busy loop with a counter that goes to a million on the JavaFX thread, it will hang your app periodically.
Similarly, don't call Thread.sleep on the JavaFX application thread, it will also hang your app periodically.

For most operations in JavaFX, you need to relinquish control of the JavaFX application thread, so that the system can take action on the commands which you have issued.  Not doing so just hangs the UI and application and, in addition, it doesn't do what you expect.  It doesn't do the first thing, then pause, then do the next thing.  Instead, the system just buffers up all the things you asked it to do, and only executes them all at once after you release control back to the system.

-----

Knute's suggestion to use a Task which runs a separate thread which does the pauses, then a runLater call to execute the event at each period, does at least fix one of the main problems occurring here by shifting the pause into its own thread rather than in the JavaFX application thread.

-----

If you want a delay for a period in JavaFX, where something happens after a period, one way to accomplish that is to use a PauseTransition with a setOnFinished call:
 https://docs.oracle.com/javase/8/javafx/api/javafx/animation/PauseTransition.html
 https://stackoverflow.com/questions/30543619/how-to-use-pausetransition-method-in-javafx


An advantage of this over a task-based approach which spawns another thread is that there is no other user thread involved, so you have less concurrent code to manage and worry about, which is pretty much always a good thing.

-----

For more complex interactions, a Timeline can be used rather than a PauseTransition.

The key thing about timeline and pause transition is that they are event-based, so you provide a callback event handler function in each case whenever the event you wish (the pause for a certain period of time) is triggered.
Due to the event handling nature of the system, there is no busy waiting or sleeping the JavaFX thread, instead, you get notified and can take action when the event is fired.

There is an example of a timeline based approach here:
https://stackoverflow.com/questions/9966136/javafx-periodic-background-task


-----

But, because there are higher-level APIs built for media processing, this is even simpler.

-----

First, for playing beep type sounds, you can use a simpler API than Media, AudioClip is built for that:
 https://docs.oracle.com/javase/9/docs/api/javafx/scene/media/AudioClip.html

If you want to repeat the same sound more than once, then you can set the cycle count on the audio clip:



-----

Now, sometimes it is a good idea to use the richer API and functionality of the Media classes rather than an AudioClip.  For example, when the Media being played is pretty long and should be buffered (unlike an AudioClip which can load the entire media into memory), or when you need the extra functionality provided by the Media API.

One such example is the double beep situation you provide in your follow-up question, where you are playing one sound, then playing another different sound after the first sound completes.
Rather than trying to do a busy wait pause of the JavaFX application thread which hangs your app, listen for an event which signals that the first media has finished playing, and use that event to trigger playing the second media.



-----

You note:
> I also consider using "aplay" system call cause so far this approach is working best on Linux
My guess is that using system calls for this kind of thing is probably not a good idea.  
You lose platform independence and you end up re-inventing the wheel because there is already decent media playback support in JavaFX.

 
Alfonso Sanz
Ranch Hand
Posts: 98
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
great and very very instructive answer!! I learnt a lot on your explanation. Thanks a lot John Damien Smith!!

PD: Indeed I had already used AudioClip.

Thanks again!
 
Don't touch me. And dont' touch this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic