• 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

AudioPlayer issues

Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, guys! I'm new in Java and I'm trying to write a small game.

What I want to do is play a sound as soon as my program runs, and stop that sound and play a different one as soon as the user clicks on "Start". The problem is that the previous sound will not stop playing, but as soon as I click on "Start" in my GUI, the new sound plays and I can hear both of them playing simultaneously.

I won't post the whole code, because I don't think it's necessary.

Here's my class for sound playback:

...and here's my game (only the important parts):

I really do not understand what is going wrong. In one of my experiments, I tried placing the "AudioPlayer.player.stop(ContinuousAudioDataStream/AudioDataStream);" inside my startSound methods of the PlaySound class, and the sound didn't start at all, because the stop() method was directly following the start() method. This proved to me that the stop() method is working.

Sorry if the code is unclear in any way and thanks in advance for any help coming my way. If you think I neglected to share any required, please let me know.


PS: Any suggestions to improve my code readability etc are welcome.
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of the com.sun... classes, I'd go with the stuff in javax.sound.sampled - it's in the API, it's much more capable and liable to be more mature.
Louis Kounios
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lester, I heeded your advice and went with javax.sound.sampled. Thanks for that!

Now that I'm almost close to finalising the basic stuff, I'm trying to finish my OO class for sound playback with the following addition: speed up/slow down the sound during playback. This is what I've done so far:

And what I get when I run my test class (with a GUI) is "Exception in thread "main" java.lang.IllegalArgumentException: Unsupported control type: Sample Rate". If I comment out both the sampleControl = (FloatControl)... and sampleControl.setValue() methods, the program runs fine. The problem is, I already tried reading the API and I do not understand what I'm doing wrong. MASTER_GAIN works like a charm.

Can anyone help me with this?
Crusading Chameleon likes the size of this ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic