-6

I'm making an audio player in java,a small code snippet that runs .wav files is:

AudioInputStream ais = AudioSystem.getAudioInputStream(new File("C:\\path\\c4.wav").getAbsoluteFile());
    Clip clip = AudioSystem.getClip();
    clip.open(ais);
    clip.start();

I want to know some things about java sounds

First,what is the use of mixers,lines,ports in java sounds.

Second,how to include other formats such as mp2,mp3...or is there a general method to include any format.

Third,how to add volume control.

Fourth,I am making this player in Net Beans so how to use slider for volume control and as seek bar.

...and I want to make the audio player using the basic java sound api,I'm reading tutorial from http://docs.oracle.com/javase/tutorial/sound/TOC.html so please don't give solutions having javafx or other media files included...

Sabre.Tooth
  • 189
  • 1
  • 3
  • 10
  • 1
    this is too general for this forum I think. we like discrete issues – d'alar'cop Aug 10 '13 at 02:29
  • Even if it is too general for this forum,I can't find a way to run mp3 files without using any external library anywhere on this forum – Sabre.Tooth Aug 10 '13 at 02:33
  • And you won't, as far as I know Java does not provide any built in support for playing mp3 files. I believe the only supported formats are wav and ogg. – Jyro117 Aug 10 '13 at 02:36
  • Jyro117 is correct. You'll need a 3rd party library for MP3 support. Google it, there are a few available. – William Morrison Aug 10 '13 at 02:40
  • Java supports AIFF, AIFF-C, AU, SND, and WAV formats via AudioFileFormat.Type: http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioFileFormat.Type.html – Commander Worf Aug 10 '13 at 02:42
  • Sabre.Tooth, if your question is closed, for the future, do research, and ask multiple questions in multiple posts. There's a lot of ground to cover here. – William Morrison Aug 10 '13 at 02:46
  • Some of your questions are answered in the [Java Sound info. page](http://stackoverflow.com/tags/javasound/info). But for future reference, the questions you ask make it sound like you want a tutor or help desk. SO is neither. Limit it to 1 question to avoid giving that impression. – Andrew Thompson Aug 10 '13 at 02:50

1 Answers1

3

First of all, if you want to play sounds that aren't looped and are longer than a few seconds, you shouldn't be using Clips.

You're going to need to use SourceDataLines, which can read audio data in several different formats (consult AudioFileFormat.Type for specifics) via streams.

As for your questions:

  1. Mixers, Lines, and Ports are all used to modify sound data as it leaves or enters the java program. That can mean changing the pitch, pan, amplitude, and so forth.

  2. In order to add MP3 decoding to your program, you're going to have to use an external library. An example would be the Java Media Framework (JMF).

  3. If you want to add volume control, you can use FloatControl.Type.VOLUME. SourceDataLines are compatible with them.

  4. To control to volume with a slider, simply get an integer value from the slider and pass it to FloatControl.Type.VOLUME. This may take a little finagling, as FloatControl.Types often have precise bounds and multipliers. As for a seek bar, you'll probably find an answer in the JMF. No guarantees, though.

Commander Worf
  • 324
  • 1
  • 9
  • Mixers, lines, and ports are much more complicated than that... – William Morrison Aug 10 '13 at 02:42
  • I know, I simplified it for the sake of brevity. – Commander Worf Aug 10 '13 at 02:42
  • Can you elaborate or at least link to the tutorials on Mixers,lines,ports? They are very complex and I feel you've oversimplified them too much. I'll +1 if that's fixed. – William Morrison Aug 10 '13 at 02:44
  • Okay, I'll elaborate. Mixer is a basic interface that allows the properties of a SourceDataLine/TargetDataLine/Clip's audio stream to be merged with other streams or modified individually. To obtain a Mixer, one usually calls `AudioSystem.getMixer()`. Line is also a basic interface that provides a "pipeline" (in the words of the API) for a mono/multichannel audio stream. That stream can either be output-oriented, as in the cast of SourceDataLines, or input-oriented, as in the case of TargetDataLines. Don't get confused by the names, as they're relative to the mixer, not the user. – Commander Worf Aug 10 '13 at 02:52
  • +1 for an attempt to give a good answer to a bad question. – asteri Aug 10 '13 at 02:58
  • Thanks worf. I do appreciate it. – William Morrison Aug 10 '13 at 03:00
  • @WilliamMorrison No problem man. Java's sound API is complicated and confusing on the best of days. – Commander Worf Aug 10 '13 at 03:01