Проблема с тем, что моя Java-программа Music Equalizer ничего не делает, когда я открываю файл .mp3.JAVA

Программисты JAVA общаются здесь
Anonymous
Проблема с тем, что моя Java-программа Music Equalizer ничего не делает, когда я открываю файл .mp3.

Сообщение Anonymous »

/
Я работаю над приложением Java Swing, которое включает в себя музыкальный эквалайзер. Предполагается, что приложение позволит пользователям выбирать файл MP3 и визуализировать уровни звука в режиме реального времени. Однако, когда я выбираю файл MP3, приложение ничего не делает и звук не воспроизводится.
Я также добавил sample.mp3 (частная статическая окончательная строка DEFAULT_MP3_PATH = "sample. mp3"), но проблема сопротивляется
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.*;
import javazoom.jl.player.*;
// import javazoom.jl.decoder.*;

public class MusicEqualizer extends JFrame {
private static final int BANDS = 32;
private static final int MAX_HEIGHT = 100;
private static final int FFT_SIZE = 1024;
private int[] levels = new int[BANDS];
private Color[] colors = new Color[BANDS];
private Player player;
private byte[] buffer;
private double[] fftData;
private boolean isPlaying = false;

public MusicEqualizer() {
setTitle("Music Equalizer");
setSize(800, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

initializeColors();
buffer = new byte[FFT_SIZE * 2];
fftData = new double[FFT_SIZE * 2];

JPanel equalizerPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawEqualizer(g);
}
};
add(equalizerPanel);

JButton selectFileButton = new JButton("Select File");
selectFileButton.addActionListener(e -> selectFile());
add(selectFileButton, BorderLayout.SOUTH);
}

private void initializeColors() {
for (int i = 0; i < BANDS; i++) {
float hue = (float) i / BANDS;
colors = Color.getHSBColor(hue, 1.0f, 1.0f);
}
}

private void drawEqualizer(Graphics g) {
int width = getWidth();
int bandWidth = width / BANDS;

for (int i = 0; i < BANDS; i++) {
int x = i * bandWidth;
int y = getHeight() - levels;
int height = levels;

g.setColor(colors);
g.fillRect(x, y, bandWidth - 1, height);
}
}

private void updateLevels(InputStream is) {
try {
int bytesRead = is.read(buffer, 0, buffer.length);

if (bytesRead > 0) {
for (int i = 0; i < FFT_SIZE * 2; i++) {
fftData = (i < bytesRead) ? buffer / 128.0 : 0.0;
}

fft(fftData);

for (int i = 0; i < BANDS; i++) {
double sum = 0;
for (int j = i * 16; j < (i + 1) * 16 && j < FFT_SIZE; j++) {
sum += Math.sqrt(fftData[2 * j] * fftData[2 * j] + fftData[2 * j + 1] * fftData[2 * j + 1]);
}
levels = (int) Math.min(MAX_HEIGHT, sum / 16 * MAX_HEIGHT);
}

repaint();
}
} catch (Exception e) {
e.printStackTrace();
}
}

private void fft(double[] x) {
int n = x.length / 2;

// bit reversal
int j = 0;
for (int i = 0; i < n - 1; i++) {
if (i < j) {
double temp = x[2*i];
x[2*i] = x[2*j];
x[2*j] = temp;
temp = x[2*i+1];
x[2*i+1] = x[2*j+1];
x[2*j+1] = temp;
}
int k = n / 2;
while (k {
try {
while (isPlaying && !player.isComplete()) {
updateLevels(bis);
Thread.sleep(20);
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();

} catch (Exception e) {
e.printStackTrace();
}
}

private void stopPlayback() {
isPlaying = false;
if (player != null) {
player.close();
player = null;
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MusicEqualizer equalizer = new MusicEqualizer();
equalizer.setVisible(true);
});
}
}


Подробнее здесь: https://stackoverflow.com/questions/786 ... open-a-mp3

Вернуться в «JAVA»