Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

audio clip will not play even though no errors are being caught

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good morning,



I know that the audio clip ahem_x.wav is correct and it is in the project folder. On my console it states:

Running the method
Playing the sound

So I know it's found the file and is not declaring any errors re the format or a null reference etc...

I followed the guidance provided on a youtube tutorial (which I can link if I'm allowed to) - every single comment seems to celebrate at last finding a simple answer to what should be a very simple question; how to play sound using Java...

Unfortunately for me it's not working and I really hope I'm missing something obvious.

I even tried surrounding the clip.open line with a try/catch to test if there was a problem opening the stream - but there wasn't.

Thanks in advance.

Stephen.

P.S. I do have the volume turned up by the way!!
 
Rancher
Posts: 954
23
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephen,

Make each line atomic--single step--don't stack any actions within the other, make each action a 1 liner and then use the result object in the next line for the call.  You are probably getting errors masked by stacking calls.

Les
 
Bartender
Posts: 331
42
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fiddled around with this once for fun. I don't know why, or if it is the right thing to do, but putting the line before the clip.start() got it to work for me. Your example works for me with this line included. I don't know anything about audio, so I can't tell you more, but you could try and see if it works.
 
Jj Roberts
Bartender
Posts: 331
42
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jj Roberts wrote:I fiddled around with this once for fun. I don't know why, or if it is the right thing to do, but putting the line before the clip.start() got it to work for me. Your example works for me with this line included. I don't know anything about audio, so I can't tell you more, but you could try and see if it works.


That is all rubbish, as I suspected it might be. This Stack Overflow thread suggests that Clip.start() is a non-blocking call, and the JVM may be exiting before the sound is played. You could have a bash at putting a sleep into the method, to keep the main thread from exiting before the sound is played. I'm just airing my ignorance here, so I'll back out now while I can  
 
Rancher
Posts: 3232
30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how I tested an audio clip:


1. I use the clip.start() so the clip can be replayed each time you click the button
2. Using the JOptionPane guarantees the JVM is still running

Note, I also tested it using a File object instead of a URL and it also worked.
 
Stephen Pillinger
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Thanks for the help and the suggestions.

It was amazing how tough it was to find guidance on how to execute a simple sound clip in Java - I'm not sure if this is something that's not normally done (for good reasons perhaps) but the solution to the problem ended up being very simple.

The code at the start of this thread was correct with one small exception - the code for the clip was executed instantaneously, not giving the clip any *time* to actually run.

By simply placing Thread.Sleep(2500) the clip had time to play.

If I didn't want to burden the CPU by using Thread.Sleep a simple Timer did the job which enabled other elements of the thread to keep running.

As such the solution to the problem was to simply amend the initial code as follows:


The above works for me and is a lot simpler than a lot of the guidance I was finding (with the exception of the one piece of advice with the above solution)!

Hope this helps and thanks all for your time.

Stephen.
 
Rob Camick
Rancher
Posts: 3232
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It was amazing how tough it was to find guidance on how to execute a simple sound clip in Java


Actually, the problem was in the context of the question.

As I demonstrated, with a complete program, the sound clip played with no problem, and there is no need for a Thread.sleeo().

You posted code in a method. We have no context of how the method is used.

Given that this question was asked in a Swing forum I would assume that you have a visible frame and you click a button or something which would result in code from the method being invoked. After all Swing is event driven. If this was the case then the code should work fine (as I demonstrated).

Since the code did not work it appears you tested it by invoking the code directly from the main() method, which doesn't really make sense in a real world application.

The point of my comment is that every question should be posted in the form of a SSCCE which demonstrates the problem so we know the exact context of how you are using the code.
 
Stephen Pillinger
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

Thanks for your reply.

I will try to provide more context in future, however, the only thing missing from my code was the main method which instigated the code - so you pretty much had all the context you needed.

I appreciate the help though - the main thing is it's working now.

Cheers.

Stephen.
 
Rob Camick
Rancher
Posts: 3232
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

however, the only thing missing from my code was the main method which instigated the code - so you pretty much had all the context you needed.


If we had the context we needed I would not have made the comment.

Again, you posted the question in a GUI related forum. The implication being that you generate the sound based on some user event.

If that is the case, then your solution to use a Thread.sleep() is unnecessary. If that code is necessary then you have some other issue somewhere else in your code.
 
Stephen Pillinger
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Rob - fair enough you're right it is GUI related and I appreciate you guys are giving help for free with this stuff so I'll do my best to present more context... I have another GUI related query so this time will try to post it as a full stand-alone program.

Appreciate your time and help - your solution using the button is, in fact, exactly how the clips will be used in the program I'm writing and I'm relieved I won't have to use Thread.sleep since from what I understand it's quite cumbersome on the CPU.
 
Marshal
Posts: 72478
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thr‍ead#sleep() isn't cumbersome on the CPU, but on your display. It stops anything happening to your display. What's more, if you have something taking a significant duration, e.g. a repaint, which can take anything up to 30ms, the sleep() can start before the action is completed. When the th‍read wakes up, it will start the next action and the repaint may never be completed.
 
Stephen Pillinger
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't realise that thanks - yeah I heard someone saying it was cumbersome on the CPU and that if you're on a single core computer it'll stop that core which basically prevents the user from doing anything else until the Thread.Sleep has finished it's thing - is that not true then?

Thank you.

Stephen.
 
Campbell Ritchie
Marshal
Posts: 72478
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it only sleeps the event dispatch thread, which you can read about here. It allows the CPU to do everything else. It is the swing display that does nothing during the sleep time.
 
Rob Camick
Rancher
Posts: 3232
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swing is single threaded. Understanding this concept is one of the most important in Swing.

I suggested you read the section from the Swing tutorial on Concurrency in Swing for more information about the Event Dispatch Thread (EDT).
 
Stephen Pillinger
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again for your help - as always, very useful.
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic