I can not start the player in my code

2019-06-09 06:30发布

I am trying to launch my my code and start the player. But I can not do that.

import javax.media.*;
import java.io.*;

public class MP3Player {

    public static void main(String[] args) throws Exception {
       File file = new File("c://player/trigger.mpg");
       MediaLocator mrl = new MediaLocator(file.toURL());
       Player player = Manager.createPlayer(mrl);
       player.start();
    }

}

[Edit by Philipp] According to a comment by the original author, Netbeans prints the following error message:

Unable to handle format: MPEG, 160x120, FrameRate=30.0, Length=28800 Failed to realize:
com.sun.media.PlaybackEngine@131f71a Error: Unable to realize
com.sun.media.PlaybackEngine@131f71a BUILD SUCCESSFUL (total time: 1 second)

[/Edit by Philipp]

标签: java jmf
2条回答
Luminary・发光体
2楼-- · 2019-06-09 06:57

Unable to handle format: MPEG, 160x120, FrameRate=30.0 It is unable to play a video stream it founds. From the description and the name of your code, the file is expected to contain only audio streams of the compression format MP3 (MPEG-1 Audio Layer III). An .mpg extension may contains lot of different mpeg formats

查看更多
相关推荐>>
3楼-- · 2019-06-09 06:59

I don't know JMF player at all, but I assume the problem is that the code exits immediately after issuing the command, terminating any other threads...

I'd try inserting a Thread.sleep(1000); after player.start(); :

public class MP3Player {
    public static void main(String[] args) throws Exception 
    {    
        File file = new File("c:/player/trigger.mpg");    
        MediaLocator mrl = new MediaLocator(file.toURL());    
        Player player = Manager.createPlayer(mrl);    
        player.start(); 
        Thread.sleep(1000);
    } 
}

If now the first second of the MP3 is heard, this was the issue.

EDIT Also, someone pointed out problems with the slashes, the path should be correct too, but the slash is not missing, but there is rather one too much of it...

EDIT2 Ok, I misread mpg for mp3, and as the poster posted the error he got: the format of the video is not supported by JMF, you need a codec.

This might be of help: Tek-tips: Play MPEG-4 movie with JMF?

查看更多
登录 后发表回答