ネットの情報を見ながら、以下のクラスを作ってみました。
SourceSound.java
java
1import java.io.*; 2import javax.sound.sampled.*; 3 4public class SourceSound{ 5 6 private static SourceDataLine line; 7 private static AudioInputStream ais; 8 private static byte[] buffer; 9 10 public SourceSound(){} 11 public SourceSound(String filename){ 12 13 setSound(filename); 14 } 15 16 public void setSound(String arg){ 17 18 File samp = new File(arg); 19 buffer = new byte[8000]; 20 try{ 21 ais = AudioSystem.getAudioInputStream(samp); 22 AudioFormat format = ais.getFormat(); 23 DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); 24 line = (SourceDataLine)AudioSystem.getLine(info); 25 line.open(format, buffer.length); 26 } catch(Exception e){ 27 28 e.printStackTrace(); 29 } 30 } 31 32 public void play(){ 33 34 try{ 35 line.start(); 36 int numBytesRead = 0; 37 while ( numBytesRead != -1){ 38 numBytesRead = ais.read(buffer, 0, buffer.length); 39 if (numBytesRead != -1){ 40 line.write(buffer, 0, numBytesRead); 41 } 42 } 43 line.drain(); 44 line.close(); 45 } catch(Exception e){ 46 47 e.printStackTrace(); 48 } 49 } 50 51 public void close(){ 52 53 line.close(); 54 } 55}
これを使うと、再生が終了するまで次の操作ができません。
だから、BGMとして再生することができません。
どこが原因でしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/31 16:39 編集
2016/02/02 14:23
2016/02/03 13:37