Pergunta

I have this code :

    package test;

import java.io.File;

import javax.media.Format;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.PlugInManager;
import javax.media.format.AudioFormat;

public class AudioTest {
    public static void main(String[] args) {
        Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
        Format input2 = new AudioFormat(AudioFormat.MPEG);
        Format output = new AudioFormat(AudioFormat.LINEAR);
        PlugInManager.addPlugIn(
            "com.sun.media.codec.audio.mp3.JavaDecoder",
            new Format[]{input1, input2},
            new Format[]{output},
            PlugInManager.CODEC
        );
        try{
            Player player = Manager.createPlayer(new MediaLocator(new File("1.mp3").toURI().toURL()));
            player.realize();
                        player.start();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

i'm trying to play an mp3 file, mp3plugin is added to the project lib as well as the jmf jar. there's no error on the console but cant hear a sound.

the file is not playing. .wav files are playing fine.

any idea ?

Foi útil?

Solução

JMF is a bad option. The project was abandoned long time ago. I have answered a similar question here:

Java - Error when trying to use mp3plugin for playing an mp3 file

it might be usefull for you - Im using Java Sound

Outras dicas

The following is all I need to play music.

public static void main(String args[]) throws NoPlayerException, CannotRealizeException, IOException {
    MediaLocator ml = new MediaLocator((new File("roar_of_future.mp3").toURL()));
    Player player = Manager.createRealizedPlayer(ml);
    player.start();
}

So please make sure mp3plugin.jar is in the classpath and your javasdk is Java 8 (32bit) or 7 (32bit) because JMF is not working on Java 9 and above.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top