So I am making a responsive audio waveform, if you don't know what that is, it is something like this: (btw this is my goal to make those audio waveforms).
https://i.ytimg.com/vi/J7uN-SQtOYI/maxresdefault.jpg
https://www.youtube.com/watch?v=OVMuwa-HRCQ
https://www.youtube.com/watch?v=SCD2tB1qILc
I don't know how to describe it, so just look at the videos and image.

but my question is, how do I get the values from the "AudioSpectrumListener", it has a lambda expression.
And I need the value of the magnitude. So I can print out the values.
Later I will use the values to make rectangles and that rectangles will change height shape because of the changing value from the magnitude.

I looked at some examples and tutorials, here are some:
jnlps://download.oracle.com/otndocs/products/javafx/8/samples/Ensemble/Ensemble.jnlp
https://victor-fx.blogspot.com/2017/08/audio-visualization-in-javafx.html

as you can see, I used animation timer, because the "AudioSpectrumInterval" is on 60, that means it updates 60 times in a second.
I can use a for a loop too, but it will print out the same values about a couple of times, or even more than that.

Recommended Answers

All 17 Replies

I just need the magnitudes to be accesable from the whole class.
(btw, I am going to remove animationtimer

The last link you posted explains how to get and understand the magnitudes, so what exactly is your question?

I just want to System.out.println(magnitudes[i]);
in the start method, but how do I even do that?
I tried this:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class AWF_Main extends Application implements AudioSpectrumListener {

    public final static String TITLE = "AWF special program";
    public final static short WIDTH = 1280;
    public final static short HEIGHT = 720;
    public static String AUDIO_FILE = "../mp3Music/Cynematic - Waterbased.mp3";
    public static String AUDIO_URI = AWF_Main.class.getResource(AUDIO_FILE).toExternalForm();

    private Stage window;
    private Scene mainS;
    private Group groupS;
    private AudioSpectrumListener asl;
    private static MediaPlayer audioMP;
    private float[] correctedMagnitude;

    @Override
    public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
        for(int i = 0; i < magnitudes.length; i++) {
            correctedMagnitude[i] = magnitudes[i] - audioMP.getAudioSpectrumThreshold();
            //System.out.println(correctedMagnitude[i]);
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        groupS = new Group();
        mainS = new Scene(groupS, WIDTH, HEIGHT, Color.DEEPPINK);
        window = primaryStage;
        window.setScene(mainS);
        window.setTitle(TITLE);
        window.show();

        // setup media
        Media audioMedia = new Media(AUDIO_URI);
        audioMP = new MediaPlayer(audioMedia);
        audioMP.setAudioSpectrumListener(this);
        play();

        //correctedMagnitude[5], why 5, because I chosed a random number.
        System.out.println(correctedMagnitude[5]);
    }

    //audioMP.setVolume(0.02); is low, because I just like to listen in low volume while I listen to other music. (:
    public void play() {
        audioMP.play();
        audioMP.setVolume(0.02);
        audioMP.setAudioSpectrumInterval(60.0);
        audioMP.setAudioSpectrumNumBands(256);
    }

    public void stop() {
        System.out.println("program exited");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

I don't want to use charts.

here is my Error:

Exception in Application start method
program exited
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v1.AWF_Main.start(AWF_Main.java:52)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
    ... 1 more
Exception running application com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v1.AWF_Main

btw, this is a link too:
jnlps://download.oracle.com/otndocs/products/javafx/8/samples/Ensemble/Ensemble.jnlp
there is a audiobarchart.

at com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v1.AWF_Main.start(AWF_Main.java:52) = System.out.println(correctedMagnitude[5]);

You can’t print those values in the start method. Start runs once and is finished before the music starts playing.
You can only access the values via the spectrum listener when the listener is called.

ps: you may want to have the listener called more that once per minute ;)

commented: how do I see when listener is called +0
commented: The old PRINT A; A=1: issue. Why did A print? +15

how do I see when listener is called

OK, you seem to be missing some basic understanding here. It's just like using a listener for someone pressing a button.

You have set this as a audio spectrum listener. Which means:

  • this contains a spectrumDataUpdate listener method
  • when the media is playing JavaFX will reguarly call your spectrumDataUpdate method at the interval you have requested (60 seconds!) passing it the audio spectrum data for the latest interval.
  • In your spectrumDataUpdate method you do whatever you want with that data, eg correct the magnitudes and pass it to another method that displays it

PS: You may want to set a lower interval. The default is 0.1 seconds.

pps: It's best if you reply with a new post rather than a comment because new comments don't get prioretised in the "latest posts" listing that I'm monitoring.

yes! Now I can finally use the values :D

this is my code:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class AWF_Main extends Application implements AudioSpectrumListener {

    public final static String TITLE = "AWF special program";
    public final static short WIDTH = 1280;
    public final static short HEIGHT = 720;
    public static String AUDIO_FILE = "../mp3Music/Cynematic - Waterbased.mp3";
    public static String AUDIO_URI = AWF_Main.class.getResource(AUDIO_FILE).toExternalForm();

    private Stage window;
    private Scene mainS;
    private Group groupS;
    private static MediaPlayer audioMP;

    @Override
    public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
        System.out.println("data updated");
        displayMag(magnitudes);
        for(int i = 0; i < magnitudes.length; i++) {
            //correctedMagnitude[i] = magnitudes[i] - audioMP.getAudioSpectrumThreshold();
//            System.out.println("0"+magnitudes[0]);
//            System.out.println("1"+magnitudes[1]);
//            System.out.println("2"+magnitudes[2]);
//            System.out.println("3"+magnitudes[3]);
//            System.out.println("4"+magnitudes[4]);
//            System.out.println("5"+magnitudes[5]);
//            System.out.println("6"+magnitudes[6]);
//            System.out.println("7"+magnitudes[7]);
//            System.out.println("8"+magnitudes[8]);
//            System.out.println("9"+magnitudes[9]);
            //System.out.println(correctedMagnitude[i]);
        }
    }

    public void displayMag(float[] mag) {
        for(int i = 0; i < mag.length; i++) {
            System.out.println(i+": " + mag[i]);
        }
        System.out.println("length of mag = " + mag.length);
    }

    public void init() {
        Media audioMedia = new Media(AUDIO_URI);
        audioMP = new MediaPlayer(audioMedia);
        play();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        groupS = new Group();
        mainS = new Scene(groupS, WIDTH/2, HEIGHT/2, Color.DEEPPINK);
        window = primaryStage;
        window.setScene(mainS);
        window.setTitle(TITLE);
        window.show();
    }

    //audioMP.setVolume(0.02); is low, because I just like to listen in low volume while I listen to other music. (:
    public void play() {
        audioMP.play();
        audioMP.setVolume(0.02);
        audioMP.setAudioSpectrumInterval(0.5);
        audioMP.setAudioSpectrumNumBands(256);
        audioMP.setAudioSpectrumListener(this);
        //AWF_newClass.printOutMagnitudes();
    }

    public void stop() {
        System.out.println("program exited");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

I thought if I set the interval to 60, I thought it would update 60 times per second and I didn't noticed it

this is what the console says:

data updated
0: -59.407444
1: -48.37642
2: -41.535774
3: -40.149982
4: -44.27457
5: -44.982887
6: -48.725376
7: -52.21941
8: -54.77891
9: -50.22617
10: -53.42569
11: -55.172703
12: -55.079517
13: -53.60956
14: -53.745964
15: -57.46172
16: -57.445312
17: -57.044395
18: -52.888622
19: -54.909782
20: -57.15198
21: -56.71712
22: -56.429665
23: -57.09065
24: -57.157146
25: -57.19809
26: -57.740284
27: -56.86342
28: -56.226715
29: -57.350414
30: -57.268696
31: -57.377327
32: -57.740887
33: -56.40759
34: -55.88547
35: -56.44446
36: -57.326187
37: -57.204216
38: -55.604618
39: -55.611687
40: -56.671505
41: -55.99083
42: -54.81242
43: -54.81779
44: -55.155308
45: -54.862213
46: -56.785095
47: -57.21616
48: -56.16005
49: -56.611744
50: -57.336994
51: -56.80366
52: -56.719086
53: -56.731297
54: -57.696148
55: -56.97476
56: -57.19966
57: -56.635822
58: -56.079693
59: -56.365444
60: -56.58281
61: -57.3104
62: -57.90639
63: -57.26793
64: -57.42998
65: -58.106003
66: -56.51674
67: -55.527695
68: -55.31978
69: -56.59834
70: -57.75076
71: -57.570744
72: -55.88267
73: -55.103874
74: -56.05077
75: -56.008892
76: -55.499
77: -56.47118
78: -56.83968
79: -57.00147
80: -56.647213
81: -56.488712
82: -55.836987
83: -56.97192
84: -57.72063
85: -58.830986
86: -58.433346
87: -57.937386
88: -58.045086
89: -57.081215
90: -56.94066
91: -57.460472
92: -57.537506
93: -57.83281
94: -58.671604
95: -58.726414
96: -58.39954
97: -57.964382
98: -59.422413
99: -58.608257
100: -56.711
101: -57.214962
102: -58.341293
103: -58.0045
104: -58.1659
105: -57.890232
106: -57.620907
107: -58.689537
108: -58.754578
109: -58.455357
110: -58.3346
111: -57.839603
112: -58.051525
113: -58.993805
114: -59.581917
115: -59.176373
116: -58.17461
117: -59.15124
118: -59.001194
119: -58.830784
120: -58.5519
121: -58.16131
122: -58.511566
123: -58.39166
124: -58.145638
125: -58.028236
126: -58.10197
127: -58.91761
128: -58.547646
129: -59.251682
130: -58.782795
131: -59.048176
132: -59.10034
133: -58.90144
134: -59.025703
135: -58.248325
136: -58.112225
137: -58.88682
138: -58.870293
139: -58.636475
140: -58.598667
141: -59.498295
142: -59.586685
143: -59.380665
144: -58.976612
145: -58.772377
146: -58.590763
147: -59.61901
148: -59.598183
149: -59.33537
150: -59.62538
151: -59.039722
152: -58.40267
153: -59.228287
154: -59.669003
155: -59.21484
156: -59.701416
157: -59.61741
158: -59.512623
159: -59.70447
160: -59.701836
161: -59.716305
162: -59.90483
163: -59.90511
164: -59.8642
165: -59.64051
166: -59.573845
167: -59.894054
168: -59.98699
169: -60.0
170: -60.0
171: -59.934967
172: -59.94722
173: -59.94488
174: -59.983204
175: -60.0
176: -59.960846
177: -59.94087
178: -59.92735
179: -59.97077
180: -60.0
181: -60.0
182: -60.0
183: -60.0
184: -60.0
185: -60.0
186: -60.0
187: -60.0
188: -60.0
189: -60.0
190: -60.0
191: -60.0
192: -60.0
193: -60.0
194: -60.0
195: -60.0
196: -60.0
197: -60.0
198: -60.0
199: -60.0
200: -60.0
201: -60.0
202: -60.0
203: -60.0
204: -60.0
205: -60.0
206: -60.0
207: -60.0
208: -60.0
209: -60.0
210: -60.0
211: -60.0
212: -60.0
213: -60.0
214: -60.0
215: -60.0
216: -60.0
217: -60.0
218: -60.0
219: -60.0
220: -60.0
221: -60.0
222: -60.0
223: -60.0
224: -60.0
225: -60.0
226: -60.0
227: -60.0
228: -60.0
229: -60.0
230: -60.0
231: -60.0
232: -60.0
233: -60.0
234: -60.0
235: -60.0
236: -60.0
237: -60.0
238: -60.0
239: -60.0
240: -60.0
241: -60.0
242: -60.0
243: -60.0
244: -60.0
245: -60.0
246: -60.0
247: -60.0
248: -60.0
249: -60.0
250: -60.0
251: -60.0
252: -60.0
253: -60.0
254: -60.0
255: -60.0
length of mag = 256
data updated
0: -58.938725
1: -46.33748
2: -37.530647
3: -32.70217
4: -37.956696
5: -44.20575
6: -49.34532
7: -52.65845
8: -52.185795
9: -48.955708
10: -51.62807
11: -51.65423
12: -51.50781
13: -50.99753
14: -51.735165
15: -54.147144
16: -54.45941
17: -56.08918
18: -50.666042
19: -53.640854
20: -55.419495
21: -55.679142
22: -56.14381
23: -54.916134
24: -56.279785
25: -57.086823
26: -56.44943
27: -54.020206
28: -54.95245
29: -55.113552
30: -56.056557
31: -57.09868
32: -55.27939
33: -55.496223
34: -56.25147
35: -55.48283
36: -56.310047
37: -55.447582
38: -55.365105
39: -56.196175
40: -56.29256
41: -55.27897
42: -54.00971
43: -53.098827
44: -53.29164
45: -53.64179
46: -54.644833
47: -56.801025
48: -55.36362
49: -55.531693
50: -55.63488
51: -55.87493
52: -55.85561
53: -54.896614
54: -56.388023
55: -56.39946
56: -56.81492
57: -55.081413
58: -53.777622
59: -54.964718
60: -54.655857
61: -56.511967
62: -56.169037
63: -55.857506
64: -56.742092
65: -56.160236
66: -54.84903
67: -54.93572
68: -55.52359
69: -57.60032
70: -56.259018
71: -56.619724
72: -55.2432
73: -53.982727
74: -54.31452
75: -53.549397
76: -53.278797
77: -54.981396
78: -54.56772
79: -55.042496
80: -54.391884
81: -53.935886
82: -52.868435
83: -55.029705
84: -55.97013
85: -55.86352
86: -56.306988
87: -56.949287
88: -57.212124
89: -56.52532
90: -56.50448
91: -56.736443
92: -56.73207
93: -56.203846
94: -56.150684
95: -55.976433
96: -57.63249
97: -56.2459
98: -56.336807
99: -56.29584
100: -55.384857
101: -54.581196
102: -57.281494
103: -57.19312
104: -58.20829
105: -56.533028
106: -55.610622
107: -57.467506
108: -57.745243
109: -56.932266
110: -56.430977
111: -57.079453
112: -57.421658
113: -58.05036
114: -58.371254
115: -58.38245
116: -57.005405
117: -57.56359
118: -57.62742
119: -58.778484
120: -58.165024
121: -57.171383
122: -57.372826
123: -56.979496
124: -57.976612
125: -57.733383
126: -57.817524
127: -58.758987
128: -58.799732
129: -58.90681
130: -58.161495
131: -58.078197
132: -58.685852
133: -58.634727
134: -58.86214
135: -57.691685
136: -57.114662
137: -58.415215
138: -58.25012
139: -58.425587
140: -58.3104
141: -59.21859
142: -58.792076
143: -58.64874
144: -58.423397
145: -58.276794
146: -58.02148
147: -59.280342
148: -59.09713
149: -59.121178
150: -59.62881
151: -58.88439
152: -58.369873
153: -58.851074
154: -59.11255
155: -58.920425
156: -59.398045
157: -59.229565
158: -58.918083
159: -59.475388
160: -59.526424
161: -59.806137
162: -59.76185
163: -59.712914
164: -59.767056
165: -59.447697
166: -59.190327
167: -59.43744
168: -59.834972
169: -59.88182
170: -59.861958
171: -59.723156
172: -59.787853
173: -59.749184
174: -59.819096
175: -59.84975
176: -59.79475
177: -59.866272
178: -59.735023
179: -59.829197
180: -59.94671
181: -59.9729
182: -59.97025
183: -59.983433
184: -60.0
185: -60.0
186: -59.953056
187: -60.0
188: -59.990234
189: -60.0
190: -59.91111
191: -60.0
192: -60.0
193: -60.0
194: -60.0
195: -60.0
196: -60.0
197: -60.0
198: -60.0
199: -60.0
200: -60.0
201: -60.0
202: -60.0
203: -60.0
204: -60.0
205: -60.0
206: -60.0
207: -60.0
208: -60.0
209: -60.0
210: -60.0
211: -60.0
212: -60.0
213: -60.0
214: -60.0
215: -60.0
216: -60.0
217: -60.0
218: -60.0
219: -60.0
220: -60.0
221: -60.0
222: -60.0
223: -60.0
224: -60.0
225: -60.0
226: -60.0
227: -60.0
228: -60.0
229: -60.0
230: -60.0
231: -60.0
232: -60.0
233: -60.0
234: -60.0
235: -60.0
236: -60.0
237: -60.0
238: -60.0
239: -60.0
240: -60.0
241: -60.0
242: -60.0
243: -60.0
244: -60.0
245: -60.0
246: -60.0
247: -60.0
248: -60.0
249: -60.0
250: -60.0
251: -60.0
252: -60.0
253: -60.0
254: -60.0
255: -60.0
length of mag = 256
data updated
0: -59.207775
1: -49.29684
2: -39.506638
3: -35.27126
4: -39.441746
5: -41.937504
6: -44.1361
7: -46.174084
8: -48.287804
9: -42.95732
10: -47.665237
11: -50.56422
12: -50.065197
13: -51.15461
14: -51.082462
15: -53.079556
16: -53.12802
17: -54.672848
18: -48.772434
19: -52.476982
20: -54.996433
21: -54.349445
22: -54.281017
23: -53.38032
24: -53.557945
25: -53.60907
26: -54.693874
27: -54.602398
28: -54.632565
29: -55.4267
30: -55.452053
31: -55.472607
32: -54.873802
33: -54.326065
34: -55.058464
35: -54.908463
36: -56.590317
37: -55.40833
38: -54.537342
39: -54.912144
40: -56.586964
41: -55.802784
42: -53.817223
43: -53.21955
44: -52.999996
45: -54.00223
46: -55.58616
47: -55.186665
48: -53.361267
49: -54.34563
50: -56.069332
51: -55.47976
52: -54.80658
53: -54.311535
54: -55.985386
55: -54.029926
56: -56.72173
57: -55.82105
58: -55.353867
59: -55.224644
60: -54.390865
61: -55.827232
62: -55.7917
63: -55.845398
64: -55.728004
65: -55.662086
66: -55.6305
67: -54.238564
68: -54.062965
69: -55.92863
70: -55.183884
71: -55.998814
72: -54.593224
73: -54.12708
74: -54.291664
75: -54.741337
76: -54.889477
77: -55.243095
78: -54.83106
79: -55.401196
80: -54.32106
81: -54.0716
82: -53.30039
83: -56.152412
84: -55.54349
85: -56.92498
86: -57.48248
87: -57.24905
88: -57.124477
89: -55.852345
90: -54.67485
91: -55.547966
92: -55.80207
93: -56.018345
94: -55.825417
95: -56.15095
96: -57.0487
97: -56.92754
98: -57.09258
99: -56.569687
100: -55.476086
101: -54.76736
102: -57.209057
103: -57.772926
104: -57.80142
105: -57.1653
106: -56.885983
107: -57.63455
108: -57.65083
109: -57.110855
110: -57.226913
111: -57.49813
112: -57.31467
113: -57.511234
114: -58.55292
115: -58.341507
116: -57.679848
117: -57.858223
118: -58.023983
119: -58.349136
120: -57.477478
121: -57.17225
122: -58.11268
123: -58.546104
124: -58.219215
125: -57.92532
126: -57.618233
127: -58.72721
128: -58.30083
129: -58.286587
130: -58.01366
131: -57.84024
132: -58.01508
133: -57.8037
134: -58.594193
135: -57.960922
136: -57.672516
137: -58.855145
138: -58.79184
139: -58.369385
140: -57.9671
141: -58.54461
142: -58.184235
143: -58.69603
144: -58.439556
145: -58.372746
146: -58.593914
147: -59.201378
148: -59.25057
149: -59.543274
150: -59.696835
151: -58.7769
152: -58.092037
153: -58.80927
154: -59.570484
155: -59.36838
156: -59.40511
157: -59.33646
158: -59.54112
159: -59.65503
160: -59.73267
161: -59.86068
162: -59.893032
163: -59.87559
164: -59.802906
165: -59.483593
166: -59.551212
167: -59.821495
168: -59.877502
169: -59.90316
170: -59.978783
171: -59.84166
172: -59.84966
173: -59.787643
174: -59.854355
175: -59.96201
176: -60.0
177: -60.0
178: -60.0
179: -60.0
180: -60.0
181: -60.0
182: -60.0
183: -60.0
184: -59.979733
185: -60.0
186: -60.0
187: -60.0
188: -60.0
189: -60.0
190: -60.0
191: -60.0
192: -60.0
193: -60.0
194: -60.0
195: -60.0
196: -60.0
197: -60.0
198: -60.0
199: -60.0
200: -60.0
201: -60.0
202: -60.0
203: -60.0
204: -60.0
205: -60.0
206: -60.0
207: -60.0
208: -60.0
209: -60.0
210: -60.0
211: -60.0
212: -60.0
213: -60.0
214: -60.0
215: -60.0
216: -60.0
217: -60.0
218: -60.0
219: -60.0
220: -60.0
221: -60.0
222: -60.0
223: -60.0
224: -60.0
225: -60.0
226: -60.0
227: -60.0
228: -60.0
229: -60.0
230: -60.0
231: -60.0
232: -60.0
233: -60.0
234: -60.0
235: -60.0
236: -60.0
237: -60.0
238: -60.0
239: -60.0
240: -60.0
241: -60.0
242: -60.0
243: -60.0
244: -60.0
245: -60.0
246: -60.0
247: -60.0
248: -60.0
249: -60.0
250: -60.0
251: -60.0
252: -60.0
253: -60.0
254: -60.0
255: -60.0
length of mag = 256
program exited

Looks like some very quiet music. (those numbers are deciBels)
While testing it will be more manageable if you have (say) 8 bands rather than 256.

I thought if I set the interval to 60, I thought it would update 60 times per second

I guessed that was the case. It's a really good example of why you should ALWAYS read the API doc for any method you're not familiar with.

I got a error from this: "public static String AUDIO_URI = AWF_Core.class.getResource(AUDIO_FILE).toExternalForm();"
I made a new class, so the code looks more cleaner and readable.
but unfortunatly I don't know what happened?

I got a error:

Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$2(LauncherImpl.java:352)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v7.v1.AWF_Core.<clinit>(AWF_Core.java:18)
    ... 11 more
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.NullPointerException
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:383)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    ... 5 more

this is how my project folders looks like in the navigator.

filesandprojectsIguess.PNG

here is my new code:

package com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v7.v1;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class AWF_Core extends Application implements AudioSpectrumListener {

    public final static String TITLE = "AWF special program";
    public final static short WIDTH = 1280;
    public final static short HEIGHT = 720;
    public static String AUDIO_FILE = "../mp3Music/Cynematic - Waterbased.mp3";
    public static String AUDIO_URI = AWF_Core.class.getResource(AUDIO_FILE).toExternalForm();

    private Stage window;
    private Scene mainS;
    private Group groupS;
    private static MediaPlayer audioMP;

    @Override
    public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
        System.out.println("data updated");
        displayMag(magnitudes);
    }

    public void displayMag(float[] mag) {
        for(int i = 0; i < mag.length; i++) {
            System.out.println(i+": " + mag[i]);
        }
        System.out.println("length of mag = " + mag.length);
    }

    public void init() {
        Media audioMedia = new Media(AUDIO_URI);
        audioMP = new MediaPlayer(audioMedia);
        play();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        groupS = new Group();
        mainS = new Scene(groupS, WIDTH/2, HEIGHT/2, Color.DEEPPINK);
        window = primaryStage;
        window.setScene(mainS);
        window.setTitle(TITLE);
        window.show();
    }

    //audioMP.setVolume(0.02); is low, because I just like to listen in low volume while I listen to other music. (:
    public void play() {
        audioMP.play();
        audioMP.setVolume(0.2);
        audioMP.setAudioSpectrumInterval(0.5);
        audioMP.setAudioSpectrumNumBands(256);
        audioMP.setAudioSpectrumListener(this);
        //AWF_newClass.printOutMagnitudes();
    }

    public void stop() {
        System.out.println("program exited");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

filesandprojectsIguess.PNG

that v7 -> v1 is not accidental. I just made a new package in v7.
if I placed the class just in v7, the error is gone and the music can play, but if the class is inside of v7.v1 then I get that error.

if I placed the class just in v7, the error is gone and the music can play, but if the class is inside of v7.v1 then I get that error.

Then just place the class in v7! Faffing around with IDEs just a distraction from learning a language or API.

ps: putting JavaFX code in init() is generally a bad idea. The JavaFX environment is not fully ready at that time. You are much safer ignoring init()and putting everything in start(...)

This is what I right now have:
Right now I am facing how to make these rectangles make more reactive like they change about 60 pixels maximal higher and I want to make it more than 60 pixels. Now I don't have a question this is just an update for my code.

package com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v_aa;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class AWF_FX_v1 extends Application implements AudioSpectrumListener {

    public final static String TITLE = "AWF special program";
    public final static short WIDTH = 1280;
    public final static short HEIGHT = 720;
    public static String AUDIO_FILE = "../mp3Music/Cynematic - Waterbased.mp3";
    public static String AUDIO_FILE1 = "../mp3Music/AcerDroid -Skies.mp3";
    public static String AUDIO_FILE2 = "../mp3Music/Capturez - Patterns.mp3";
    public static String AUDIO_FILE3 = "../mp3Music/Kanooli - Instinct.mp3";
    public static String AUDIO_FILE4 = "../mp3Music/RavenKreyn_InTheAir_NCSrelease.mp3";
    public static String AUDIO_URI = AWF_FX_v1.class.getResource(AUDIO_FILE2).toExternalForm();

    private Stage window;
    private Scene mainS;
    private Group groupS;
    private MediaPlayer audioMP;
    private float[] cMag;

//  private Rectangle rect0;
//  private Rectangle rect1;
//  private Rectangle rect2;
//  private Rectangle rect3;
//  private Rectangle rect4;
//  private Rectangle rect5;
//  private Rectangle rect6;
//  private Rectangle rect7;

    private Rectangle[] rect = new Rectangle[8];

    @Override
    public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
        for (int i = 0; i < magnitudes.length; i++) {
            cMag = magnitudes;
            cMag[i] = (int) cMag[i];
            //System.out.println((i + 1) + ": " + (cMag[i] * -1));
        }
        for(int i = 0; i < rect.length; i++) {
            rect[i].setY(310 + ((cMag[i] + 60) * -1));
            rect[i].setHeight(50 + (cMag[i] + 60));
            System.out.println(i + " setY: " + rect[i].getY());
            System.out.println(i + " setHeight " + rect[i].getHeight());
        }

//ignore these comments down below, I tried two times how I did the rectangles thingies.        

//      //second try failed...
//      
////        rect0.setY(HEIGHT / 2 - 50 + (60 - cMag[0]));
////        rect0.setHeight(50 + (60 - cMag[0]));
////        System.out.println("rect0.setY: " + (HEIGHT / 2 - 50 + (60 - cMag[0])) + "\nrect0.setHeight: " + (50 + (60 - cMag[0])));
////        
////        rect1.setY(HEIGHT / 2 - 50 + (60 - cMag[1]));
////        rect1.setHeight(50 + (60 - cMag[1]));
////        System.out.println("rect1.setY: " + (HEIGHT / 2 - 50 + (60 - cMag[1])) + "\nrect1.setHeight: " + (50 + (60 - cMag[1])));
////        
////        rect2.setY(HEIGHT / 2 - 50 + (60 - cMag[2]));
////        rect2.setHeight(50 + (60 - cMag[2]));
////        System.out.println("rect2.setY: " + (HEIGHT / 2 - 50 + (60 - cMag[2])) + "\nrect2.setHeight: " + (50 + (60 - cMag[2])));
////        
////        rect3.setY(HEIGHT / 2 - 50 + (60 - cMag[3]));
////        rect3.setHeight(50 + (60 - cMag[3]));
////        System.out.println("rect3.setY: " + (HEIGHT / 2 - 50 + (60 - cMag[3])) + "\nrect3.setHeight: " + (50 + (60 - cMag[3])));
////        
////        rect4.setY(HEIGHT / 2 - 50 + (60 - cMag[4]));
////        rect4.setHeight(50 + (60 - cMag[4]));
////        System.out.println("rect4.setY: " + (HEIGHT / 2 - 50 + (60 - cMag[4])) + "\nrect4.setHeight: " + (50 + (60 - cMag[4])));
////        
////        rect5.setY(HEIGHT / 2 - 50 + (60 - cMag[5]));
////        rect5.setHeight(50 + (60 - cMag[5]));
////        System.out.println("rect5.setY: " + (HEIGHT / 2 - 50 + (60 - cMag[5])) + "\nrect5.setHeight: " + (50 + (60 - cMag[5])));
////        
////        rect6.setY(HEIGHT / 2 - 50 + (60- cMag[6]));
////        rect6.setHeight(50 + (60 - cMag[6]));
////        System.out.println("rect6.setY: " + (HEIGHT / 2 - 50 + (60 - cMag[6])) + "\nrect6.setHeight: " + (50 + (60 - cMag[6])));
////        
////        rect7.setY(HEIGHT / 2 - 50 + (60 - cMag[7]));
////        rect7.setHeight(50 + (60 - cMag[7]));
////        System.out.println("rect7.setY: " + (HEIGHT / 2 - 50 + (60 - cMag[7])) + "\nrect7.setHeight: " + (50 + (60 - cMag[7])));
////        System.out.println();
//      
//      //first try failed...
//      
////        rect0.setTranslateY(100 + (cMag[0] * -1));
////        rect1.setTranslateY(100 + (cMag[1] * -1));
////        rect2.setTranslateY(100 + (cMag[2] * -1));
////        rect3.setTranslateY(100 + (cMag[3] * -1));
////        rect4.setTranslateY(100 + (cMag[4] * -1));
////        rect5.setTranslateY(100 + (cMag[5] * -1));
////        rect6.setTranslateY(100 + (cMag[6] * -1));
////        rect7.setTranslateY(100 + (cMag[7] * -1));
////        
////        rect0.setHeight(50 + ((cMag[0] * -1)  ));
////        rect1.setHeight(50 + (cMag[1] * -1));
////        rect2.setHeight(50 + (cMag[2] * -1));
////        rect3.setHeight(50 + (cMag[3] * -1));
////        rect4.setHeight(50 + (cMag[4] * -1));
////        rect5.setHeight(50 + (cMag[5] * -1));
////        rect6.setHeight(50 + (cMag[6] * -1));
////        rect7.setHeight(50 + (cMag[7] * -1));
    }

    @Override
    public void start(Stage primaryStage) {
        groupS = new Group();
        mainS = new Scene(groupS, WIDTH, HEIGHT, Color.DARKBLUE);
        window = primaryStage;
        window.setScene(mainS);
        window.setTitle(TITLE);
        window.show();

        Media audioMedia = new Media(AUDIO_URI);
        audioMP = new MediaPlayer(audioMedia);
        audioMP.play();
        audioMP.setCycleCount(MediaPlayer.INDEFINITE);
        audioMP.setVolume(0.02);
        audioMP.setAudioSpectrumInterval(0.01);
        audioMP.setAudioSpectrumNumBands(8);
        audioMP.setAudioSpectrumListener(this);

//      rect0 = new Rectangle(250, HEIGHT / 2 - 50, 50, 50);
//      rect0.setFill(Color.AZURE);
//      rect1 = new Rectangle(350, HEIGHT / 2 - 50, 50, 50);
//      rect1.setFill(Color.CORNFLOWERBLUE);
//      rect2 = new Rectangle(450, HEIGHT / 2 - 50, 50, 50);
//      rect2.setFill(Color.SPRINGGREEN);
//      rect3 = new Rectangle(550, HEIGHT / 2 - 50, 50, 50);
//      rect3.setFill(Color.YELLOWGREEN);
//      rect4 = new Rectangle(650, HEIGHT / 2 - 50, 50, 50);
//      rect4.setFill(Color.YELLOW);
//      rect5 = new Rectangle(750, HEIGHT / 2 - 50, 50, 50);
//      rect5.setFill(Color.ORANGE);
//      rect6 = new Rectangle(850, HEIGHT / 2 - 50, 50, 50);
//      rect6.setFill(Color.RED);
//      rect7 = new Rectangle(950, HEIGHT / 2 - 50, 50, 50);
//      rect7.setFill(Color.BLACK);

        rect[0] = new Rectangle(250, HEIGHT / 2 - 50, 50, 50);
        rect[0].setFill(Color.AZURE);
        rect[1] = new Rectangle(350, HEIGHT / 2 - 50, 50, 50);
        rect[1].setFill(Color.CORNFLOWERBLUE);
        rect[2] = new Rectangle(450, HEIGHT / 2 - 50, 50, 50);
        rect[2].setFill(Color.SPRINGGREEN);
        rect[3] = new Rectangle(550, HEIGHT / 2 - 50, 50, 50);
        rect[3].setFill(Color.YELLOWGREEN);
        rect[4] = new Rectangle(650, HEIGHT / 2 - 50, 50, 50);
        rect[4].setFill(Color.YELLOW);
        rect[5] = new Rectangle(750, HEIGHT / 2 - 50, 50, 50);
        rect[5].setFill(Color.ORANGE);
        rect[6] = new Rectangle(850, HEIGHT / 2 - 50, 50, 50);
        rect[6].setFill(Color.RED);
        rect[7] = new Rectangle(950, HEIGHT / 2 - 50, 50, 50);
        rect[7].setFill(Color.BLACK);

//      groupS.getChildren().add(rect0);
//      groupS.getChildren().add(rect1);
//      groupS.getChildren().add(rect2);
//      groupS.getChildren().add(rect3);
//      groupS.getChildren().add(rect4);
//      groupS.getChildren().add(rect5);
//      groupS.getChildren().add(rect6);
//      groupS.getChildren().add(rect7);

        for(int i = 0; i < rect.length; i++) {
            groupS.getChildren().add(rect[i]);
//          System.out.println(i);
        }
    }

    public void stop() {
        System.out.println("program exited");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

code without that comments lines:

package com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v_aa;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class AWF_FX_v1 extends Application implements AudioSpectrumListener {

    public final static String TITLE = "AWF special program";
    public final static short WIDTH = 1280;
    public final static short HEIGHT = 720;
    public static String AUDIO_FILE = "../mp3Music/Cynematic - Waterbased.mp3";
    public static String AUDIO_FILE1 = "../mp3Music/AcerDroid -Skies.mp3";
    public static String AUDIO_FILE2 = "../mp3Music/Capturez - Patterns.mp3";
    public static String AUDIO_FILE3 = "../mp3Music/Kanooli - Instinct.mp3";
    public static String AUDIO_FILE4 = "../mp3Music/RavenKreyn_InTheAir_NCSrelease.mp3";
    public static String AUDIO_URI = AWF_FX_v1.class.getResource(AUDIO_FILE2).toExternalForm();

    private Stage window;
    private Scene mainS;
    private Group groupS;
    private MediaPlayer audioMP;
    private float[] cMag;

    private Rectangle[] rect = new Rectangle[8];

    @Override
    public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
        for (int i = 0; i < magnitudes.length; i++) {
            cMag = magnitudes;
            cMag[i] = (int) cMag[i];
        }
        for(int i = 0; i < rect.length; i++) {
            rect[i].setY(310 + ((cMag[i] + 60) * -1));
            rect[i].setHeight(50 + (cMag[i] + 60));
            System.out.println(i + " setY: " + rect[i].getY());
            System.out.println(i + " setHeight " + rect[i].getHeight());
        }
    }

    @Override
    public void start(Stage primaryStage) {
        groupS = new Group();
        mainS = new Scene(groupS, WIDTH, HEIGHT, Color.DARKBLUE);
        window = primaryStage;
        window.setScene(mainS);
        window.setTitle(TITLE);
        window.show();

        Media audioMedia = new Media(AUDIO_URI);
        audioMP = new MediaPlayer(audioMedia);
        audioMP.play();
        audioMP.setCycleCount(MediaPlayer.INDEFINITE);
        audioMP.setVolume(0.02);
        audioMP.setAudioSpectrumInterval(0.01);
        audioMP.setAudioSpectrumNumBands(8);
        audioMP.setAudioSpectrumListener(this);

        rect[0] = new Rectangle(250, HEIGHT / 2 - 50, 50, 50);
        rect[0].setFill(Color.AZURE);
        rect[1] = new Rectangle(350, HEIGHT / 2 - 50, 50, 50);
        rect[1].setFill(Color.CORNFLOWERBLUE);
        rect[2] = new Rectangle(450, HEIGHT / 2 - 50, 50, 50);
        rect[2].setFill(Color.SPRINGGREEN);
        rect[3] = new Rectangle(550, HEIGHT / 2 - 50, 50, 50);
        rect[3].setFill(Color.YELLOWGREEN);
        rect[4] = new Rectangle(650, HEIGHT / 2 - 50, 50, 50);
        rect[4].setFill(Color.YELLOW);
        rect[5] = new Rectangle(750, HEIGHT / 2 - 50, 50, 50);
        rect[5].setFill(Color.ORANGE);
        rect[6] = new Rectangle(850, HEIGHT / 2 - 50, 50, 50);
        rect[6].setFill(Color.RED);
        rect[7] = new Rectangle(950, HEIGHT / 2 - 50, 50, 50);
        rect[7].setFill(Color.BLACK);

        for(int i = 0; i < rect.length; i++) {
            groupS.getChildren().add(rect[i]);
        }
    }

    public void stop() {
        System.out.println("program exited");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Nailed it :D

package com.VINSTORM.bleidorb.AudioWaveFormFX_V1.v_aa;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class AWF_FX_v5 extends Application implements AudioSpectrumListener {

    public final static String TITLE = "AWF special program";
    public final static short WIDTH = 1280;
    public final static short HEIGHT = 720;
    public static String AUDIO_FILE = "../mp3Music/Cynematic - Waterbased.mp3";
    public static String AUDIO_FILE1 = "../mp3Music/AcerDroid -Skies.mp3";
    public static String AUDIO_FILE2 = "../mp3Music/Capturez - Patterns.mp3";
    public static String AUDIO_FILE3 = "../mp3Music/Kanooli - Instinct.mp3";
    public static String AUDIO_FILE4 = "../mp3Music/RavenKreyn_InTheAir_NCSrelease.mp3";
    public static String AUDIO_URI = AWF_FX_v1.class.getResource(AUDIO_FILE1).toExternalForm();

    private Stage window;
    private Scene mainS;
    private Group groupS;
    private MediaPlayer audioMP;
    private float[] cMag;
    private short power = 5;

    private Rectangle[] rect = new Rectangle[40];

    @Override
    public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {
        for (int i = 0; i < magnitudes.length; i++) {
            cMag = magnitudes;
            cMag[i] = (int) cMag[i] * power;
            System.out.println("cMag (" + i + "): " + cMag[i]);
        }

        for(int i = 0; i < rect.length; i++) {
            rect[i].setY(500 + ((cMag[i] + (60 * power)) * -1));
            rect[i].setHeight(50 + (cMag[i] + (60 * power)));
        }
    }

    @Override
    public void start(Stage primaryStage) {
        groupS = new Group();
        mainS = new Scene(groupS, WIDTH, HEIGHT, Color.DARKBLUE);
        window = primaryStage;
        window.setScene(mainS);
        window.setTitle(TITLE);
        window.show();

        Media audioMedia = new Media(AUDIO_URI);
        audioMP = new MediaPlayer(audioMedia);
        audioMP.play();
        audioMP.setCycleCount(MediaPlayer.INDEFINITE);
        audioMP.setVolume(0.02);
        audioMP.setAudioSpectrumInterval(0.01);
        audioMP.setAudioSpectrumNumBands(40);
        audioMP.setAudioSpectrumListener(this);

        for(int i = 0; i < rect.length; i++) {
            rect[i] = new Rectangle();
            rect[i].setX(250 + (i * 20));
            rect[i].setY(500);
            rect[i].setWidth(10);
            rect[i].setHeight(50);
            rect[i].setFill(Color.hsb((360 / 40 * i), 0.8, 1));
            groupS.getChildren().add(rect[i]);
        }
    }

    public void stop() {
        System.out.println("program exited");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Great! Well done.
JC

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.