在Jframe java中播放媒体文件



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

"从源读取时出错。"

我觉得我的媒体URL是正确的。这是一个小型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
new URL("C:\Users\Michael\Downloads\mike.jar")

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

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

最新更新