2

我在 jar 文件中嵌入资源(媒体文件)时遇到问题。JButton我所做的是我创建了一个 java 播放器,当我选择它时将播放URL媒体java文件位于,也就是说,当我制作一个jar并将其分发给我的客户时,他们也可以播放视频文件,因为所有资源都嵌入在jar中,所以当我在Netbeans中运行应用程序时一切正常,但是当我制作一个 jar 文件,当所有资源都打包在 jar 中时,它也不会播放媒体文件。

下面是我的源代码,其中包含 main

Manish.java

package javamedia;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Manish implements ActionListener {

    JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12;
    JFrame frame;
    JLabel label;
    JPanel panel;
    ImageIcon img1;
    boolean flag = true;

    public Manish() {
        frame = new JFrame();
        panel = new JPanel();
        panel.setLayout(null);
        frame.setSize(800, 500);
        frame.setTitle("Practical Video's");
        panel.setBackground(Color.GRAY);
        //label settng
        label.setBounds(0, 0, 785, 75);
        panel.add(label);
        btn1.setBounds(500, 100, 100, 25);
        panel.add(btn1);
        btn1.addActionListener(this);
        frame.add(panel);
        frame.setVisible(true);


    }

    public static void main(String[] args) {
        new Manish();
    }

    public void actionPerformed(ActionEvent evt) {
        Client2 cl;
        if (evt.getSource() == btn1) {
            cl = new Client2();

            URL mediaURL = this.getClass().getResource("resources/z.mpg");
            String file = mediaURL.toString();

            cl.show();
            cl.VedioAudioPlayer(file);
        }



    }
}

Client2.java(播放器代码)

  package javamedia;

 import java.util.*;
 import java.net.*;
 import java.io.*;
 import javax.media.*;
 import javax.swing.*;


import java.awt.*;
 public class Client2 extends javax.swing.JFrame implements
        ControllerListener, Runnable {

    String filepath;
    Player player;
    Component visualComponent;
    Component controlComponent;
    Component progressBar;
    boolean firstTime;
    long CachingSize;
    int controlPanelHeight;
    int videoWidth;
    int videoHeight;

    /**
     * Creates new form Client2
     */
    public Client2() {
        initComponents();
// setBounds(50,50,880,600);
        setBounds(0, 0, 400, 325);

    }

    public void run() {
        while (true) {
        }
    }

    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel1 = new javax.swing.JPanel();
        getFile = new javax.swing.JButton();
        panel = new java.awt.Panel();
        getContentPane().setLayout(null);
        setTitle("Falcon Video Player");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setLayout(null);
        jPanel1.setBackground(new java.awt.Color(51, 51, 51));
        getFile.setBackground(new java.awt.Color(204, 255, 204));
        getFile.setForeground(new java.awt.Color(51, 51, 255));
        getFile.setText("PlayFile");
        getFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getFileActionPerformed(evt);
            }
        });


        getFile.setBounds(260, 130, 72, 26);
        panel.setLayout(null);
        panel.setName("panel");
        jPanel1.add(panel);
        panel.setBounds(15, 15, 700, 650);
        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 880, 580);

        pack();
    }

    private void FilesActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void peersActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void ExitButtonActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }

    private void getFileActionPerformed(java.awt.event.ActionEvent evt) {
    }

    /**
     * Exit the Application
     */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        player.stop();
        new Manish().frame.setVisible(false);

    }

    public void VedioAudioPlayer(String file) {


        System.out.println("The Asking File is ready to Play");
        player = null;
        visualComponent = null;
        controlComponent = null;
        progressBar = null;
        firstTime = true;
        CachingSize = 0L;
        controlPanelHeight = 0;
        videoWidth = 0;
        videoHeight = 0;
        filepath = file;
        init(filepath);

    }

    public void init(String filepath) {

        MediaLocator medialocator = null;
        Object obj = null;
        if (filepath == null) {
            Fatal("Invalid media file parameter");
        }
        try {
            if ((medialocator = new MediaLocator(filepath)) == null) {
                Fatal("Can't build URL for " + filepath);
            }
            try {
                player = Manager.createPlayer(medialocator);
                player.start();
                player.addControllerListener(this);
                player.realize();
            } catch (NoPlayerException noplayerexception) {
                System.out.println(noplayerexception);
                Fatal("Could not create player for " + medialocator);
            }

        } catch (MalformedURLException _ex) {
            Fatal("Invalid media file URL!");
        } catch (IOException _ex) {
            Fatal("IO exception creating player for " + medialocator);
        }
    }

    public synchronized void controllerUpdate(ControllerEvent controllerevent) {
        if (player == null) {
            return;
        }
        if (controllerevent instanceof RealizeCompleteEvent) {
            if (progressBar != null) {
                panel.remove(progressBar);
                progressBar = null;
            }
            int i = 320;
            int j = 0;
            if (controlComponent == null && (controlComponent = player.getControlPanelComponent())
                    != null) {
                controlPanelHeight = controlComponent.getPreferredSize().height;
                panel.add(controlComponent);
                j += controlPanelHeight;
            }
            if (visualComponent == null && (visualComponent = player.getVisualComponent()) != null) {
                panel.add(visualComponent);
                Dimension dimension = visualComponent.getPreferredSize();
                videoWidth = dimension.width;
                videoHeight = dimension.height;
                i = videoWidth;
                j += videoHeight;
                visualComponent.setBounds(0, 0, videoWidth, videoHeight);
            }
            if (controlComponent != null) {
                controlComponent.setBounds(0, videoHeight, i, controlPanelHeight);
                controlComponent.invalidate();
            }
        } else if (controllerevent instanceof CachingControlEvent) {
            if (player.getState() > 200) {
                return;
            }
            CachingControlEvent cachingcontrolevent = (CachingControlEvent) controllerevent;
            CachingControl cachingcontrol = cachingcontrolevent.getCachingControl();
            if (progressBar == null && (progressBar = cachingcontrol.getControlComponent()) != null) {
                panel.add(progressBar);
                panel.setSize(progressBar.getPreferredSize());
                validate();
            }
        } else if (controllerevent instanceof EndOfMediaEvent) {
            player.setMediaTime(new Time(0L));
            player.stop();
        } else if (controllerevent instanceof ControllerErrorEvent) {
            player = null;
            Fatal(((ControllerErrorEvent) controllerevent).getMessage());
        } else if (controllerevent instanceof ControllerClosedEvent) {
            panel.removeAll();
        }
    }

    void Fatal(String s) {
        System.err.println("FATAL ERROR: " + s);
        throw new Error(s);
    }

    public void destroy() {
        if (player != null) {
            player.close();
        }
    }

    public void start() {
        if (player != null) {
            player.start();
        }
    }

    public void stop() {
        if (player != null) {
            player.stop();
            player.deallocate();
        }
    }
    private javax.swing.JButton getFile;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    public java.awt.Panel panel;
    // End of variables declaration//GEN-END:variables
}
4

1 回答 1

0

首先确保您的资源确实正确打包在 jar 文件中。然后请记住,当从 jar 中读取文件时,情况可能会略有不同。我认为问题出在以下几行。

URL mediaURL = this.getClass().getResource("resources/z.mpg");
String file = mediaURL.toString();

mediaURL.toString() 可能不是一个合适的解决方案。也许您应该考虑将 InputStream 获取到文件中,例如:

URL mediaURL = getClass().getResource("/resources/z.mpg"); 
InputStream file = mediaURL.openStream();

然后尝试使用 String 的 InputStream 来引用你想要的资源

于 2013-05-28T21:46:11.983 回答