Asked  6 Months ago    Answers:  5   Viewed   23 times

I want to be able to play sound files in my program. Where should I look?

 Answers

79

I wrote the following code that works fine. But I think it only works with .wav format.

public static synchronized void playSound(final String url) {
  new Thread(new Runnable() {
  // The wrapper thread is unnecessary, unless it blocks on the
  // Clip finishing; see comments.
    public void run() {
      try {
        Clip clip = AudioSystem.getClip();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(
          Main.class.getResourceAsStream("/path/to/sounds/" + url));
        clip.open(inputStream);
        clip.start(); 
      } catch (Exception e) {
        System.err.println(e.getMessage());
      }
    }
  }).start();
}
Tuesday, June 1, 2021
 
akohout
answered 6 Months ago
52

..any ideas how to fix this exception, ..

Since WAV is a container format, it might include any number of encoding internally.

..wav files can be encoded with a variety of codecs to reduce the file size (for example the GSM or MP3 codecs).

See the Java Sound info. page for details no how to add support for an extra encoding (using the SPI), and a lead on an MP3 SPI.

..or if there's another way to get file duration ?

Not really. With compressed formats, bit rate is variable and there is no way to know for sure how long a track takes short of converting it to PCM or similar formats, in which the bit rate is constant & the track time is completely resolved.

Saturday, June 26, 2021
 
Jauco
answered 6 Months ago
98

You can use &lt; for < and &gt; for > .

Tuesday, June 29, 2021
 
max_
answered 5 Months ago
17

Maybe later (or never) you can use the actual experimental pipeline operator |>, which has the following syntax:

expression |> function

Your wanted result could be achieved by taking the functions as separate functions and iterate the stream array for each pipe.

This works only in FF. From version 58: this feature is behind the --enable-pipeline-operator compile flag.

const
    a = x => { x = x * x; console.log("map1=" + x); return x; },
    b = x => { x = x * 3; console.log("map2=" + x); return x; },
    c = x => console.log("forEach=" + x)

var nums = [1, 2, 3, 4, 5, 6];

nums.forEach(v => v |> a |> b |> c);

The same with a pipe as function (function composition enabling piping) with a closure over the wanted functions.

const
    pipe = (...functions) => input => functions.reduce((acc, fn) => fn(acc), input),
    a = x => { x = x * x; console.log("map1=" + x); return x; },
    b = x => { x = x * 3; console.log("map2=" + x); return x; },
    c = x => console.log("forEach=" + x)

var nums = [1, 2, 3, 4, 5, 6],
    pipeline = pipe(a, b, c);

nums.forEach(pipeline);
Thursday, August 5, 2021
 
sober
answered 4 Months ago
27

Are you sure you're reading 8-bit mono audio? Otherwise one byte does not equal one sample, and you cannot just scale each byte. E.g. if it is 16-bit data you have to parse every pair of bytes as a 16-bit integer, scale that, and then write it back as two bytes.

Wednesday, September 15, 2021
 
g.pickardou
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share