简体   繁体   中英

can't play audio File with JMF

I have a problem with playing an MP3 file with JMF, it displays the following error :

Error: 
Unable to realize com.sun.media.amovie.AMController@80669d Exception in thread "main"

javax.media.CannotRealizeException at javax.media.Manager.blockingCall(Manager.java:2005) at
javax.media.Manager.createRealizedPlayer(Manager.java:528) at tp.Main.main(Main.java:44) 
Error value: 80070020

here is my code :

public class Main {

    public static void main(String[] args) throws NoPlayerException, CannotRealizeException, MalformedURLException, IOException, URISyntaxException {


        Fenetre F1 = new Fenetre();
        F1.setVisible(true);

        InputStream is = Main.class.getClass().getResourceAsStream("/data/gmu.mp3");
        File temp=File.createTempFile("temp", ".mp3");
        OutputStream os = new FileOutputStream(temp);
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = is.read(bytes)) != -1) {
            os.write(bytes, 0, read);
        }
        final Player p=Manager.createRealizedPlayer(temp.toURI().toURL());

        p.start();
        while(true){
            if(p.getMediaTime().getSeconds()==p.getDuration().getSeconds()){
                p.stop();
                p.setMediaTime(new Time(0));
                p.start();
            }
        }
    }
}

normally it works If I don't use the InputStream, and use simply

File f =new File(Main.class.getResource("/data/gmu.mp3").getFile());
final Player p=Manager.createRealizedPlayer(temp.toURI().toURL());

but this way It doesn't work when I pack my JAR file, so I'm trying to use InputStream, the aim is to make a JAR with a WORKING music

Actually I just had to convert my MP3 file to WAV and it worked !!! I don't know why the player exception was caught with the MP3 temporary file but nothing goes wrong with WAV. this is not really the answer I sought since the WAV files takes a lot more space but at least it works.

I also tried OGG format, which makes the same problem of MP3 ...

any other answer, suggestion, or discussion is welcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM