2017-02-04 79 views
0

我想在調用方法getoutput時播放音樂,並在調用完成時停止播放音樂。我能夠做到前者而不是後者。完成調用getoutput方法後,如何停止音樂?如何停止Java中的音頻?

import java.io.InputStream; 
import javax.sound.sampled.AudioSystem; 

import sun.audio.AudioPlayer; 
import sun.audio.AudioStream; 

public class Student { 

    public void music() { 

     try { 
      // open the sound file as a Java input stream 
      String gongFile = "C:\\Users\\wei liang\\Documents\\Programming fundamentals\\T7-Arrays\\Assignment\\TT.wav"; 
      InputStream in = new FileInputStream(gongFile); 

      // create an audiostream from the inputstream 
      AudioStream audioStream = new AudioStream(in); 

      // play the audio clip with the audioplayer class 
      AudioPlayer.player.start(audioStream); 

     } catch (Exception ex) { 
      System.out.println("Error! Can't find file."); 
      ex.printStackTrace(); 
     } 
    } 
} 

這是我調用音樂方法和其他方法在Student類中未顯示的主要方法。

public class StudentUser { 

    //Main method 
    public static void main(String args[]) { 

     //creating a new Student object 
     Student stud = new Student(); 

     //Calling the music method 
     stud.music(); 

     //Calling the getoutput method 
     stud.getoutput(); 

    } 
} 
+0

不要使用'太陽*'包,他們是無證和隨時都可能消失。您應該使用'AudioInputStream'和'Clip'來存儲/播放音頻。 – Brendan

+0

如果你需要答案讓我知道,我寫信給你,你沒有迴應。 –

回答

0

我會建議使用線程,線程將保持直播,直到音樂文件播放,它會自動結束。

由於您沒有提供getoutput方法,您只需撥打music方法即可播放音樂並結束播放。

Student類將是這樣的:

import javax.sound.sampled.*; 
import java.io.File; 

class Student { 

    public synchronized void music(final String fileName, final SoundOptions mode) { 
     Thread music = new Thread(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(
          new File(fileName)); 

        final Clip audioLineClip = (Clip) AudioSystem.getLine(
          new Line.Info(Clip.class)); 
        audioLineClip.open(inputStream); 
        audioLineClip.setFramePosition(0); 

        audioLineClip.addLineListener(new LineListener() { 
         @Override 
         public void update(LineEvent event) { 
          LineEvent.Type type = event.getType(); 
          if (type == LineEvent.Type.OPEN) { 
          } else if (type == LineEvent.Type.CLOSE) { 
           System.exit(0); 
          } else if (type == LineEvent.Type.START) { 
          } else if (type == LineEvent.Type.STOP) { 
           audioLineClip.close(); 
          } 
         } 
        }); 

        switch (mode) { 
         case Stop: 
          audioLineClip.stop(); 
          break; 
         case Play: 
          audioLineClip.start(); 
          break; 
         case Loop: 
          audioLineClip.loop(Clip.LOOP_CONTINUOUSLY); 
          break; 
        } 
       } catch (Exception e) { 
        System.err.println(e.getMessage()); 
       } 
      } 
     }); 

     if (mode != SoundOptions.Stop) { 
      music.start(); 

      synchronized (music) { 
       while (true) { 
        try { 
         music.wait(); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 

     } else { 
      music.interrupt(); 
     } 
    } 


} 

enum SoundOptions { 
    Play, Loop, Stop 
} 

StudentUser類:

public class StudentUser { 

    private static String gongFile = "C:\\Users\\wei liang\\Documents\\Programming fundamentals\\T7-Arrays\\Assignment\\TT.wav"; 

    //Main method 
    public static void main(String args[]) { 

     //Creating a new Student object 
     Student stud = new Student(); 

     //Calling the music method and it is stops when music ends 
     stud.music(gongFile, SoundOptions.Play); 
    } 
}