如何在一个单独的线程中调用(JAVE LIBRARY)的编码方法?

时间:2015-11-21 14:41:58

标签: java multithreading swing nullpointerexception illegalstateexception

我正在编写一个java应用程序,用于将音频文件从mp3转换为mp3或其他格式。我正在使用JAVE库。 Encoder类的编码方法是一种阻塞方法。所以,我试图在一个单独的线程中运行它。它压缩成功,但它给出了空指针异常。

请告诉我在新线程中调用encode方法的正确方法。如何避免IllegalStateException?

run方法抛出NPE

<html>
  <head>
    <title>Title</title>
  </head>
  <body>
    <a href="www.google.com">I'm a link to somewhere</a>
  </body>
</html>

Class&#39;构造函数

public class DayTwo extends JFrame implements ActionListener, Runnable
{

public static JFrame frame = new JFrame("Audio Compression Utility");

Thread myThread;

String[] bitRateValues = {"64000", "128000", "160000", "192000", "256000","320000"};
String samplingRateValues[] = {"44100", "48000","24000", "96000"};
String[] formatValues = {"wav", "mp3"};
JFileChooser sourceFileOpen = new JFileChooser();

String sourceStr = new String(), targetStr = new String() ;
 File source = null,target = null;

 Encoder encoder = new Encoder();
 EncodingAttributes attrs = new EncodingAttributes();
 AudioAttributes audio = new AudioAttributes();

 //After that i have declared JButtons , Jlabels etc..
 ...
  }

这是构造函数

中的 run方法
DayTwo()
{
super("Audio Compression");
setLookAndFeel();
setBounds(50,50,850,650);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
browseTargetFileBtn.setEnabled(false);

以下是&#34;压缩&#34;&#39;按钮

myThread = new Thread(new Runnable()
        {
    public void run()
    {
        try
        {
            encoder.encode(source, target, attrs);
            JOptionPane.showMessageDialog(frame, "Success");
        }catch(Exception e)
        {
                 e.printStackTrace();
        }

    }
    });

以下是&#34;浏览&#34;的操作执行方法用户选择输出文件夹时的按钮

ActionListener actionListenerObjectForCompression =new ActionListener()
{
      @Override public void actionPerformed(ActionEvent e) {
          try
          {  
                audio.setCodec("libmp3lame");
                audio.setBitRate( 96000 ); //96,000 = 96 kbit per sec
                audio.setSamplingRate(44100);

                attrs.setFormat("mp3");
                attrs.setAudioAttributes(audio);

                myThread.start();
          }
          catch(Exception je)
          {
             je.printStackTrace();
          }
          }
};

以下是&#34;浏览&#34;的操作执行方法用户选择源文件时按钮

ActionListener actionListenerObjectForTargetLocation = new ActionListener()
{
        @Override public void actionPerformed(ActionEvent e) 
        {
            JFileChooser targetFileOpen = new JFileChooser();
            if (targetFileOpen.showSaveDialog(null) == JFileChooser.APPROVE_OPTION)
            {

                targetStr = targetFileOpen.getSelectedFile().getPath();
                browseTargetFileTextField.setText(targetStr);
                target = new File(targetStr);
            }
        }
};

主要方法:

ActionListener actionListenerObjectForSelectFile = new ActionListener()
{

public void actionPerformed(ActionEvent e) 
{

    if (sourceFileOpen.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
    {
        browseTargetFileBtn.setEnabled(true);
        sourceStr = sourceFileOpen.getSelectedFile().getPath();
        browseTargetFileBtn.setEnabled(true);
        source = new  File(sourceStr);
        browseSourceFileTextField.setText(sourceFileOpen.getSelectedFile().getPath());
    }
}
};

}

public static void main(String[] args)
{
 DayTwo ob = new DayTwo();
}

1 个答案:

答案 0 :(得分:2)

多线程问题的最佳解决方案是使用SwingWorker 它基本上将工作分为后台阶段,然后发生GUI更新阶段 一个基本的例子:

   class ProperDecoder extends SwingWorker<Object, Object> {
       @Override
       public Object doInBackground() {
           return decode();
       }

       @Override
       protected void done() {
           try {
               Object decoderResult = get();
               updateGUI();
           } catch (Exception ignore) {
             // catch errors
           }
       }
   }

   (new ProperDecoder()).execute();