Red de conocimiento informático - Material del sitio web - Cómo instalar mp3spi1.9.4.jar

Cómo instalar mp3spi1.9.4.jar

El código de referencia es el siguiente

Primero descargue el paquete de software para reproducir mp3, como mp3spi1.9.4.jar. Agregue este paquete al proyecto.

El código de demostración del reproductor es el siguiente

paquete com.test.audio;

importar java.io.File;

importar java.awt.BorderLayout;

importar java.awt.FileDialog; // Método de procesamiento de doble clic

if (e.getClickCount() == 2) {

// Reproduce el archivo seleccionado

filename = list.getSelectedItem() {

// Reproduce el archivo seleccionado getSelectedItem()

play(

}

}

});

agregar(lista, "Centro"); > // Visualización de información

Panel panel = new Panel(new GridLayout(2, 1));

labelfilepath = new Label("Dir:"); p> labelfilename = new Label("Archivo:");

Panel.

add(labelfilepath);

panel.add(labelfilename);

add(panel, "North");<

// Registrar evento de cierre de ventana

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

setVisible(true);

}

// Abrir

privado vacío abierto () {

diálogo FileDialog = new FileDialog(this, "Abrir", 0);

dialog.setVisible(true);

filepath = diálogo. getDirectory();

if (filepath! = null) {

labelfilepath.setText("Dir: " filepath);

// Mostrar lista de archivos

list.removeAll();

Archivo filedir = new File(filepath);

Archivo[] filelist = filedir.listFiles();

for (Archivo archivo: lista de archivos) {

String nombre de archivo = file.getName().toLowerCase();

if (filename.endsWith(". mp3") || nombre de archivo.endsWith(".wav")) {

list.add(nombre de archivo);

}

}

}

}

}

// jugar

privado void jugar() {

try {

isStop = true; // detiene el hilo de reproducción

// espera a que se detenga el hilo de reproducción

System.out.print("Start : " nombre de archivo);

while (!hasStop) {

System.out.print(".") )

prueba {

Thread.sleep(10);

} catch (Excepción e) {

}

}

System.out. println("");

Archivo archivo = nuevo Archivo(ruta de archivo nombre de archivo);

labelfilename.setText("Reproduciendo:" nombre de archivo);

p>

//

Obtenga el flujo de entrada del archivo

audioInputStream = AudioSystem! = AudioFormat.Encoding.PCM_SIGNED) {

audioFormat = nuevo AudioFormat(AudioFormat.Encoding.PCM_SIGNED,

audioFormat.getSampleRate(), 16, audioFormat

. getChannels(), audioFormat.getChannels() * 2,

audioFormat.getSampleRate(), false

audioInputStream = AudioSystem.getLine(dataLineInfo

sourceDataLine.open(audioFormat);

sourceDataLine.start();

// Crea un hilo separado para la reproducción

isStop = false;

p>

Hilo playThread = new Thread(new PlayThread());

playThread.start()

} catch (Excepción e) {

<; p > e.printStackTrace();

}

}

clase PlayThread extiende el hilo {

byte tempBuffer[] = nuevo byte [320];

ejecución nula pública() {

prueba {

int cnt

hasStop = false

// leer datos en caché

while ((cnt = audioInputStream.read(tempBuffer, 0,

tempBuffer.length)) ! = -1) {

if (isStop)

descanso

if (cnt gt; 0) {

// Escribir datos en caché

sourceDataLine.write(tempBuffer, 0, cnt);

}

}

// El bloque espera la temperatura los datos se generarán como vacíos

sourceDataLine.drain();

sourceDataLine.close()

hasStop = true; } captura (Excepción e) {

e.printStackTrace();

System.exit(0);

}

}

público estático vacío principal (String args[]

) {

nuevo reproductor de música();

}

}

}

}