在Jframe java中播放媒体文件

时间:2013-07-18 20:24:27

标签: java video jframe media-player media

我想在Jframe中播放视频并将其附加到必须播放的jpanel(jPanel1)。它一直在说:

  

“从源头读取错误。”

我觉得我的媒体网址是对的。这是一个小型MP4视频。

这是我的代码:

public void Player()  {

    try{
        //create a player to play the media specified in the URL


        Player mediaPlayer = Manager.createRealizedPlayer(new URL("C:\\Users\\Michael\\Downloads\\mike.jar"));

        Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);

        //get the components for the video and the playback controls
        Component video = mediaPlayer.getVisualComponent();
        Component controls = mediaPlayer.getControlPanelComponent();

        if ( video != null )
            jPanel1.add( video, BorderLayout.CENTER ); //add video component
        if ( controls != null )
            jPanel1.add( controls, BorderLayout.SOUTH ); //add controls

            mediaPlayer.start(); //start playing the media clip
    } //end try
    catch ( NoPlayerException noPlayerException ){
        JOptionPane.showMessageDialog(null, "No media player found");
    } //end catch
    catch ( CannotRealizeException cannotRealizeException ){
        JOptionPane.showMessageDialog(null, "Could not realize media player.");
    } //end catch
    catch ( IOException iOException ){
        JOptionPane.showMessageDialog(null, "Error reading from the source.");
    } //end catch

1 个答案:

答案 0 :(得分:2)

new URL("C:\\Users\\Michael\\Downloads\\mike.jar")

这不是您想要创建指向本地文件的URL的方式。使用

new File(path).toURI().toURL()