2013-06-12 44 views
-1

我使用的mp3plugin.jar庫和我的音樂效果很好。唯一的問題是,當我的程序啓動時,JFrame不顯示。在我加入音樂,它的工作perfectly.This是我對音樂代碼:當音樂播放時JFrame不顯示?

package com.org.pong; 
import javax.sound.sampled.*; 
import javax.sound.*; 
import java.io.*; 
import java.net.URL; 

public class Music { 

    Music(String url) { 

     int total, totalToRead, numBytesRead, numBytesToRead; 
     byte[] buffer; 
     boolean stopped; 
     AudioFormat wav; 
     TargetDataLine line; 
     SourceDataLine lineIn; 
     DataLine.Info info; 
     File file; 
     FileInputStream fis; 

     // AudioFormat(float sampleRate, int sampleSizeInBits, 
     // int channels, boolean signed, boolean bigEndian) 
     wav = new AudioFormat(44100, 16, 2, true, false); 
     info = new DataLine.Info(SourceDataLine.class, wav); 

     buffer = new byte[1024 * 333]; 
     numBytesToRead = 1024 * 333; 
     total = 0; 
     stopped = false; 

     if (!AudioSystem.isLineSupported(info)) { 
      System.out.print("no support for " + wav.toString()); 
     } 
     try { 
      // Obtain and open the line. 
      lineIn = (SourceDataLine) AudioSystem.getLine(info); 
      lineIn.open(wav); 
      lineIn.start(); 
      fis = new FileInputStream(file = new File(url)); 
      totalToRead = fis.available(); 

      while (total < totalToRead && !stopped) { 
       numBytesRead = fis.read(buffer, 0, numBytesToRead); 
       if (numBytesRead == -1) 
       break; 
       total += numBytesRead; 
       lineIn.write(buffer, 0, numBytesRead); 
      } 

     } catch (LineUnavailableException ex) { 
      ex.printStackTrace(); 
     } catch (FileNotFoundException nofile) { 
      nofile.printStackTrace(); 
     } catch (IOException io) { 
      io.printStackTrace(); 
     } 
    } 

} 
+1

這很有趣,我告訴ü,這將前面的問題post..try使用OCUR'SwingWorker' – nachokk

+0

這一點都不好笑。實際上有點難過。 –

+0

@nachokk對不起。我沒有注意到我的其他線程。 –

回答

2

這表示您阻止了事件分派主題。這將阻止任何UI元素更新,直到您停止屏蔽。

只有看了你的代碼,我建議你的罪魁禍首是在這裏...

while (total < totalToRead && !stopped) { 
    numBytesRead = fis.read(buffer, 0, numBytesToRead); 
    if (numBytesRead == -1) 
    break; 
    total += numBytesRead; 
    lineIn.write(buffer, 0, numBytesRead); 
} 

嘗試創建一個後臺線程中,你要麼運行Music類或在它自己的運行循環後臺線程。

看看Concurrency in Swing更多細節

與工作示例

這是一個相當原始的例子更新。通常我會有一個Thread這將負責播放音樂,但可能會中斷並播放另一首歌曲,但這僅僅是一個概念證明。

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.DataLine; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.SourceDataLine; 
import javax.sound.sampled.TargetDataLine; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestMusic { 

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

    public TestMusic() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       Music.play("/play/some/music/white/boy"); 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new JLabel("Look Ma, no hands")); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public static class Music { 

     public static void play(final String url) { 

      new Thread(new Runnable() { 

       @Override 
       public void run() { 
        new Music(url); 
       } 
      }).start(); 

     } 

     private int total, totalToRead, numBytesRead, numBytesToRead; 
     private byte[] buffer; 
     private boolean stopped; 
     private AudioFormat wav; 
     private TargetDataLine line; 
     private SourceDataLine lineIn; 
     private DataLine.Info info; 
     private File file; 
     private FileInputStream fis; 

     public Music(String url) { 


      // AudioFormat(float sampleRate, int sampleSizeInBits, 
      // int channels, boolean signed, boolean bigEndian) 
      wav = new AudioFormat(44100, 16, 2, true, false); 
      info = new DataLine.Info(SourceDataLine.class, wav); 

      buffer = new byte[1024 * 333]; 
      numBytesToRead = 1024 * 333; 
      total = 0; 
      stopped = false; 

      if (!AudioSystem.isLineSupported(info)) { 
       System.out.print("no support for " + wav.toString()); 
      } 
      try { 
       // Obtain and open the line. 
       lineIn = (SourceDataLine) AudioSystem.getLine(info); 
       lineIn.open(wav); 
       lineIn.start(); 
       fis = new FileInputStream(file = new File(url)); 
       totalToRead = fis.available(); 

       while (total < totalToRead && !stopped) { 
        numBytesRead = fis.read(buffer, 0, numBytesToRead); 
        if (numBytesRead == -1) { 
         break; 
        } 
        total += numBytesRead; 
        lineIn.write(buffer, 0, numBytesRead); 
       } 

      } catch (LineUnavailableException ex) { 
       ex.printStackTrace(); 
      } catch (FileNotFoundException nofile) { 
       nofile.printStackTrace(); 
      } catch (IOException io) { 
       io.printStackTrace(); 
      } 
     } 
    } 
} 
+0

那麼我只需要將一個線程添加到我的音樂課程並啓動它? –

+0

這是一種可能性 – MadProgrammer

+0

我試過了,結果是一樣的。 –

2

這是代碼的長期運行位阻斷Swing事件分派線程的一個典型例子。這將導致GUI凍結,因爲該線程不可用於繪製組件或與用戶交互。解決方案:爲音樂使用後臺線程,以免阻塞GUI的事件線程。

+0

+1 Tag - Bet me:P – MadProgrammer

+0

@MadProgrammer:但你的更完整。 1+ –

+0

@HovercraftFullOfEels:我將如何製作後臺線程? –