Foros del Web » Programación para mayores de 30 ;) » Java »

Java y mp3

Estas en el tema de Java y mp3 en el foro de Java en Foros del Web. Hola! Mi pregunta es muy simple: ¿Cómo reproducir archivos mp3 con java? Si alguien sabe, me sería de mucha ayuda! Gracias...
  #1 (permalink)  
Antiguo 05/11/2005, 11:23
Avatar de Sith_Vader  
Fecha de Ingreso: enero-2005
Mensajes: 43
Antigüedad: 19 años, 4 meses
Puntos: 0
Pregunta Java y mp3

Hola!

Mi pregunta es muy simple:

¿Cómo reproducir archivos mp3 con java?
Si alguien sabe, me sería de mucha ayuda!
Gracias
__________________
Que la fuerza esté con nosotros !!! :si:
  #2 (permalink)  
Antiguo 07/11/2005, 05:40
Avatar de Ani Alamo  
Fecha de Ingreso: julio-2005
Mensajes: 35
Antigüedad: 18 años, 9 meses
Puntos: 1
Código útil

Aqui te copio un código que funciona y consiste en ejecutar un fichero MP3 remoto con Java:

Código PHP:
/*
* BasicPlayerTest.

* JavaZOOM : [email protected]
* http://www.javazoom.net
*
*-----------------------------------------------------------------------
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*----------------------------------------------------------------------
*/
package javazoom.jlgui.basicplayer;

import java.io.File;
import java.io.PrintStream;
import java.util.Map;

/**
* This class implements a simple player based on BasicPlayer.
* BasicPlayer is a threaded class providing most features
* of a music player. BasicPlayer works with underlying JavaSound 
* SPIs to support multiple audio formats. Basically JavaSound supports
* WAV, AU, AIFF audio formats. Add MP3 SPI (from JavaZOOM) and Vorbis
* SPI( from JavaZOOM) in your CLASSPATH to play MP3 and Ogg Vorbis file.
*/
public class BasicPlayerTest implements BasicPlayerListener
{
private 
PrintStream out null;

/**
* Entry point.
* @param args filename to play.
*/
public static void main(String[] args)
{
BasicPlayerTest test = new BasicPlayerTest();
test.play(args[0]); 
}

/**
* Contructor.
*/
public BasicPlayerTest()
{
out System.out;
}

public 
void play(String filename)
{
// Instantiate BasicPlayer.
BasicPlayer player = new BasicPlayer();
// BasicPlayer is a BasicController.
BasicController control = (BasicControllerplayer;
// Register BasicPlayerTest to BasicPlayerListener events.
// It means that this object will be notified on BasicPlayer
// events such as : opened(...), progress(...), stateUpdated(...)
player.addBasicPlayerListener(this);

try

// Open file, or URL or Stream (shoutcast) to play.
control.open(new File(filename));
// control.open(new URL("http://yourshoutcastserver.com:8000"));

// Set Volume (0 to 1.0).
control.setGain(0.85);

// Set Pan (-1.0 to 1.0).
control.setPan(0.0);

// Start playback in a thread.
control.play();

// If you want to pause/resume/pause the played file then
// write a Swing player and just call control.pause(),
// control.resume() or control.stop(). 
// Use control.seek(bytesToSkip) to seek file
// (i.e. fast forward and rewind). seek feature will
// work only if underlying JavaSound SPI implements
// skip(...). True for MP3SPI (JavaZOOM) and SUN SPI's
// (WAVE, AU, AIFF).

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

/**
* Open callback, stream is ready to play.
*
* properties map includes audio format dependant features such as
* bitrate, duration, frequency, channels, number of frames, vbr flag, ... 
*
* @param stream could be File, URL or InputStream
* @param properties audio stream properties.
*/
public void opened(Object streamMap properties)
{
// Pay attention to properties. It's useful to get duration, 
// bitrate, channels, even tag such as ID3v2.
display("opened : "+properties.toString()); 
}

/**
* Progress callback while playing.

* This method is called severals time per seconds while playing.
* properties map includes audio format features such as
* instant bitrate, microseconds position, current frame number, ... 

* @param bytesread from encoded stream.
* @param microseconds elapsed (<b>reseted after a seek !</b>).
* @param pcmdata PCM samples.
* @param properties audio stream parameters.
*/
public void progress(int bytesreadlong microsecondsbyte[] pcmdataMap properties)
{
// Pay attention to properties. It depends on underlying JavaSound SPI
// MP3SPI provides mp3.equalizer.
display("progress : "+properties.toString());
}

/**
* Notification callback for basicplayer events such as opened, eom ...

* @param event
*/
public void stateUpdated(BasicPlayerEvent event)
{
// Notification of BasicPlayer states (opened, playing, end of media, ...)
display("stateUpdated : "+event.toString());
}

/**
* A handle to the BasicPlayer, plugins may control the player through
* the controller (play, stop, ...)
* @param controller : a handle to the player
*/ 
public void setController(BasicController controller)
{
display("setController : "+controller);
}

public 
void display(String msg)
{
if (
out != nullout.println(msg);
}


Un saludo.
__________________

Ani Alamo Gómez de González


"Sin Dios no soy nada en este mundo, sin El nada puedo ser... ni las hojas de los árboles se mueven, sino es por su gran poder. Bendito seas Señor."
  #3 (permalink)  
Antiguo 07/11/2005, 06:36
 
Fecha de Ingreso: febrero-2005
Mensajes: 396
Antigüedad: 19 años, 2 meses
Puntos: 1
La anterior respuesta es un poco cutre... Un copy / paste mal hecho. Intenta localizar el paquete que contiene las clases que se necesitan en el ejemplo visitando su web http://www.javazoom.net, donde seguro que viene una documentación más extensa sobre el tema.

Un saludo

Zerjillo
  #4 (permalink)  
Antiguo 07/11/2005, 09:27
Avatar de Sith_Vader  
Fecha de Ingreso: enero-2005
Mensajes: 43
Antigüedad: 19 años, 4 meses
Puntos: 0
Gracias amigos!
Lo intentaré.
__________________
Que la fuerza esté con nosotros !!! :si:
  #5 (permalink)  
Antiguo 07/11/2005, 14:40
 
Fecha de Ingreso: junio-2005
Mensajes: 286
Antigüedad: 18 años, 10 meses
Puntos: 2
si quieres ser mas directo y no usar clases externas, puedes usar el API del Java Media Framework (creo que no es actualizado frecuentemente). Ademas de MP3, puedes reproducir videos MPEG.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 11:50.