Tocando MP3 usando a API de Java Sound

Você pode por favor sugerir que como eu posso escrever uma peça que toca uma música?

Eu tentei o seguinte trecho, mas eu recebo a exceção: insira a descrição da imagem aqui

import sun.audio.*; import java.io.*; class tester { public static void main(String args[]) throws Exception { InputStream in=new FileInputStream("tester.mp3"); AudioStream as=new AudioStream(in); AudioPlayer.player.start(as); } } 

Como mencionado, o Java Sound não suporta MP3 por padrão. Para os tipos que ele suporta em qualquer JRE específico, verifique AudioSystem.getAudioFileTypes() .

Uma maneira de adicionar suporte para leitura de MP3 é adicionar o mp3plugin.jar 1 baseado em JMF ao caminho de class de tempo de execução do aplicativo.

  1. Esse link é conhecido por ser menos do que totalmente confiável. O Jar também está disponível na minha unidade de compartilhamento de software .

Quanto à sua pergunta real, enquanto um javax.sound.sampled.Clip pode parecer ideal para este tipo de tarefa, infelizmente, só vai segurar um segundo de som estéreo, 16 bits, 44,1kHz. É por isso que desenvolvi o BigClip . (Que tem seus próprios problemas com looping, se você corrigi-los, informe novamente.)

 package org.pscode.xui.sound.bigclip; import java.awt.Component; import javax.swing.*; // J2SE 1.3 import javax.sound.sampled.*; import java.io.*; // J2SE 1.4 import java.util.logging.*; import java.util.Arrays; /** An implementation of the javax.sound.sampled.Clip that is designed to handle Clips of arbitrary size, limited only by the amount of memory available to the app. It uses the post 1.4 thread behaviour (daemon thread) that will stop the sound running after the main has exited. 
  • 2012-07-24 - Fixed bug in size of byte array (2^16 -> (int)Math.pow(2, 16)).
  • 2009-09-01 - Fixed bug that had clip ..clipped at the end, by calling drain() (before calling stop()) on the dataline after the play loop was complete. Improvement to frame and microsecond position determination.
  • 2009-08-17 - added convenience constructor that accepts a Clip. Changed the private convertFrameToM..seconds methods from 'micro' to 'milli' to reflect that they were dealing with units of 1000/th of a second.
  • 2009-08-14 - got rid of flush() after the sound loop, as it was cutting off tracks just before the end, and was found to be not needed for the fast-forward/rewind functionality it was introduced to support.
  • 2009-08-11 - First binary release.
NB Remove @Override notation and logging to use in 1.3+ @since 1.5 @version 2009-08-17 @author Andrew Thompson */ public class BigClip implements Clip, LineListener { /** The DataLine used by this Clip. */ private SourceDataLine dataLine; /** The raw bytes of the audio data. */ private byte[] audioData; /** The stream wrapper for the audioData. */ private ByteArrayInputStream inputStream; /** Loop count set by the calling code. */ private int loopCount; /** Internal count of how many loops to go. */ private int countDown; /** The start of a loop point. Defaults to 0. */ private int loopPointStart; /** The end of a loop point. Defaults to the end of the Clip. */ private int loopPointEnd; /** Stores the current frame position of the clip. */ private int framePosition; /** Thread used to run() sound. */ private Thread thread; /** Whether the sound is currently playing or active. */ private boolean active; /** Stores the last time bytes were dumped to the audio stream. */ private long timelastPositionSet; private int bufferUpdateFactor = 2; /** The parent Component for the loading progress dialog. */ Component parent = null; /** Used for reporting messages. */ private Logger logger = Logger.getAnonymousLogger(); /** Default constructor for a BigClip. Does nothing. Information from the AudioInputStream passed in open() will be used to get an appropriate SourceDataLine. */ public BigClip() {} /** There are a number of AudioSystem methods that will return a configured Clip. This convenience constructor allows us to obtain a SourceDataLine for the BigClip that uses the same AudioFormat as the original Clip. @param clip Clip The Clip used to configure the BigClip. */ public BigClip(Clip clip) throws LineUnavailableException { dataLine = AudioSystem.getSourceDataLine( clip.getFormat() ); } /** Provides the entire audio buffer of this clip. @return audioData byte[] The bytes of the audio data that is loaded in this Clip. */ public byte[] getAudioData() { return audioData; } /** Sets a parent component to act as owner of a "Loading track.." progress dialog. If null, there will be no progress shown. */ public void setParentComponent(Component parent) { this.parent = parent; } /** Converts a frame count to a duration in milliseconds. */ private long convertFramesToMilliseconds(int frames) { return (frames/(long)dataLine.getFormat().getSampleRate())*1000; } /** Converts a duration in milliseconds to a frame count. */ private int convertMillisecondsToFrames(long milliseconds) { return (int)(milliseconds/dataLine.getFormat().getSampleRate()); } @Override public void update(LineEvent le) { logger.log(Level.FINEST, "update: " + le ); } @Override public void loop(int count) { logger.log(Level.FINEST, "loop(" + count + ") - framePosition: " + framePosition); loopCount = count; countDown = count; active = true; inputStream.reset(); start(); } @Override public void setLoopPoints(int start, int end) { if ( start<0 || start>audioData.length-1 || end<0 || end>audioData.length ) { throw new IllegalArgumentException( "Loop points '" + start + "' and '" + end + "' cannot be set for buffer of size " + audioData.length); } if (start>end) { throw new IllegalArgumentException( "End position " + end + " preceeds start position " + start); } loopPointStart = start; framePosition = loopPointStart; loopPointEnd = end; } @Override public void setMicrosecondPosition(long milliseconds) { framePosition = convertMillisecondsToFrames(milliseconds); } @Override public long getMicrosecondPosition() { return convertFramesToMilliseconds(getFramePosition()); } @Override public long getMicrosecondLength() { return convertFramesToMilliseconds(getFrameLength()); } @Override public void setFramePosition(int frames) { framePosition = frames; int offset = framePosition*format.getFrameSize(); try { inputStream.reset(); inputStream.read(new byte[offset]); } catch(Exception e) { e.printStackTrace(); } } @Override public int getFramePosition() { long timeSinceLastPositionSet = System.currentTimeMillis() - timelastPositionSet; int size = dataLine.getBufferSize()*(format.getChannels()/2)/bufferUpdateFactor; int framesSinceLast = (int)((timeSinceLastPositionSet/1000f)* dataLine.getFormat().getFrameRate()); int framesRemainingTillTime = size - framesSinceLast; return framePosition - framesRemainingTillTime; } @Override public int getFrameLength() { return audioData.length/format.getFrameSize(); } AudioFormat format; @Override public void open(AudioInputStream stream) throws IOException, LineUnavailableException { AudioInputStream is1; format = stream.getFormat(); if (format.getEncoding()!=AudioFormat.Encoding.PCM_SIGNED) { is1 = AudioSystem.getAudioInputStream( AudioFormat.Encoding.PCM_SIGNED, stream ); } else { is1 = stream; } format = is1.getFormat(); InputStream is2; if (parent!=null) { ProgressMonitorInputStream pmis = new ProgressMonitorInputStream( parent, "Loading track..", is1); pmis.getProgressMonitor().setMillisToPopup(0); is2 = pmis; } else { is2 = is1; } byte[] buf = new byte[ (int)Math.pow(2, 16) ]; int totalRead = 0; int numRead = 0; ByteArrayOutputStream baos = new ByteArrayOutputStream(); numRead = is2.read( buf ); while (numRead>-1) { baos.write( buf, 0, numRead ); numRead = is2.read( buf, 0, buf.length ); totalRead += numRead; } is2.close(); audioData = baos.toByteArray(); AudioFormat afTemp; if (format.getChannels()<2) { afTemp = new AudioFormat( format.getEncoding(), format.getSampleRate(), format.getSampleSizeInBits(), 2, format.getSampleSizeInBits()*2/8, // calculate frame size format.getFrameRate(), format.isBigEndian() ); } else { afTemp = format; } setLoopPoints(0,audioData.length); dataLine = AudioSystem.getSourceDataLine(afTemp); dataLine.open(); inputStream = new ByteArrayInputStream( audioData ); } @Override public void open(AudioFormat format, byte[] data, int offset, int bufferSize) throws LineUnavailableException { byte[] input = new byte[bufferSize]; for (int ii=0; ii0) && active ) { logger.log(Level.FINEST, "BigClip.start() loop " + framePosition ); totalBytes += bytesRead; int framesRead; byte[] tempData; if (format.getChannels()<2) { tempData = convertMonoToStereo(data, bytesRead); framesRead = bytesRead/ format.getFrameSize(); bytesRead*=2; } else { framesRead = bytesRead/ dataLine.getFormat().getFrameSize(); tempData = Arrays.copyOfRange(data, 0, bytesRead); } framePosition += framesRead; if (framePosition>=loopPointEnd) { framePosition = loopPointStart; inputStream.reset(); countDown--; logger.log(Level.FINEST, "Loop Count: " + countDown ); } timelastPositionSet = System.currentTimeMillis(); byte[] newData; if (fastForward) { newData = getEveryNthFrame(tempData, 2); } else if (fastRewind) { byte[] temp = getEveryNthFrame(tempData, 2); newData = reverseFrames(temp); inputStream.reset(); totalBytes -= 2*bytesRead; framePosition -= 2*framesRead; if (totalBytes<0) { setFastRewind(false); totalBytes = 0; } inputStream.skip(totalBytes); logger.log(Level.INFO, "totalBytes " + totalBytes); } else { newData = tempData; } dataLine.write(newData, 0, newData.length); if (startOrMove) { data = new byte[bufSize/ bufferUpdateFactor]; startOrMove = false; } } logger.log(Level.FINEST, "BigClip.start() loop ENDED" + framePosition ); active = false; dataLine.drain(); dataLine.stop(); /* should these open()/close() be here, or explicitly called by user program? */ dataLine.close(); } catch (LineUnavailableException lue) { logger.log( Level.SEVERE, "No sound line available!", lue ); if (parent!=null) { JOptionPane.showMessageDialog( parent, "Clear the sound lines to proceed", "No audio lines available!", JOptionPane.ERROR_MESSAGE); } } } }; thread= new Thread(r); // makes thread behaviour compatible with JavaSound post 1.4 thread.setDaemon(true); thread.start(); } /** Assume the frame size is 4. */ public byte[] reverseFrames(byte[] data) { byte[] reversed = new byte[data.length]; byte[] frame = new byte[4]; for (int ii=0; ii(data.length/4)-5) { logger.log(Level.FINER, "From \t" + first + " \tlast " + last ); logger.log(Level.FINER, "To \t" + ((ii*4)+0) + " \tlast " + ((ii*4)+3) ); } } /* for (int ii=0; iilargest) { largest = Math.abs(current); } } } else { for (int cc = 0; cc < samples; cc++) { current = (audioData[cc*2+1]*256 + (audioData[cc*2] & 0xFF)); if (Math.abs(current)>largest) { largest = Math.abs(current); } } } } else { for (int cc = 0; cc < samples; cc++) { current = (audioData[cc] & 0xFF); if (Math.abs(current)>largest) { largest = Math.abs(current); } } } } else { if (bitDepth/8==2) { if (bigEndian) { for (int cc = 0; cc < samples; cc++) { current = (audioData[cc*2]*256 + (audioData[cc*2+1] - 0x80)); if (Math.abs(current)>largest) { largest = Math.abs(current); } } } else { for (int cc = 0; cc < samples; cc++) { current = (audioData[cc*2+1]*256 + (audioData[cc*2] - 0x80)); if (Math.abs(current)>largest) { largest = Math.abs(current); } } } } else { for (int cc = 0; cc < samples; cc++) { if ( audioData[cc]>0 ) { current = (audioData[cc] - 0x80); if (Math.abs(current)>largest) { largest = Math.abs(current); } } else { current = (audioData[cc] + 0x80); if (Math.abs(current)>largest) { largest = Math.abs(current); } } } } } // audioData logger.log(Level.FINEST, "Max signal level: " + (double)largest/(Math.pow(2, bitDepth-1))); return (double)largest/(Math.pow(2, bitDepth-1)); } }

Sun Sound API não reproduz MP3

Quando eu preciso, eu uso a biblioteca JavaZOOM jLayer (http://www.javazoom.net/javalayer/javalayer.html).

Para poder tocar mp3 você ainda precisa adicionar o MP3SPI (fornecido com jLayer).

Eu tentei fazer uma edição com base nos bugs que eu encontrei com o código BigClip, no entanto, foi rejeitado. O código a seguir é minhas correções para o programa. Lembre-se de que parte disso ainda não foi testada, pois eu não estava realmente preocupado com looping, avanço rápido ou retrocesso.

Editar: executou testes e atualizou o código, pois não estava funcionando com o retrocesso / avanço rápido. Agora funciona, mas ainda não testou o loop, embora teoricamente deva funcionar.

Obtenha o código BigClip atualizado aqui: http://pastie.org/4523763

Converta o arquivo para .wav usando o serviço online do media.io ; então use-o em seu programa.

  String file="SOME//SONGFILE//PATH"; FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); player = new Player(bis); player.play(); 

As pastas jar que você deve ter: mp3plugin.jar e jlayer.jar (do google)

Eu não acredito que a class java AudioStream suporta mp3 nativamente. De acordo com este link, ele suporta apenas AIFF, AU e WAV. Você terá que procurar em outra biblioteca para carregar e reproduzir um arquivo mp3.

Este site descreve como reproduzir um arquivo mp3 em Java