简体   繁体   中英

Playing a media file in a Jframe java

I want to play a video inside a Jframe and am attaching it to a jpanel (jPanel1) where it must play. It keeps saying:

"Error reading from the source."

And I feel that my media URL is right. It's a small MP4 video.

This is my code:

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")

This is not how you want to be creating a URL that points to a local file. Use

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

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