Advertisement
Guest User

Untitled

a guest
Oct 11th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. package sign;
  2.  
  3. import java.util.Queue;
  4. import java.util.LinkedList;
  5. import java.io.File;
  6. import javax.sound.midi.*;
  7.  
  8. public class PlayMusicInOrder {
  9.  
  10.     final Sequencer seq;
  11.     private Queue<String> song_queue;
  12.  
  13.     public PlayMusicInOrder (Queue<String> song_queue) {
  14.         Sequencer seq = null;
  15.         try{
  16.             seq = MidiSystem.getSequencer();
  17.             seq.addMetaEventListener( new MetaEventListener()
  18.             {
  19.                 public void meta(MetaMessage msg){
  20.                     if(msg.getType()==0x2F)
  21.                     {
  22.                         PlayMusicInOrder.this.play_next_song();
  23.                     }
  24.                 }
  25.             });
  26.         }
  27.         catch (Exception e){}
  28.         this.seq=seq;
  29.         this.song_queue=song_queue;
  30.  
  31.     }
  32.  
  33.     private void play_next_song(){
  34.         String next_song = song_queue.remove();
  35.         System.out.printf("Creating sequence for %s", next_song);
  36.         try{
  37.             Sequence sequence = MidiSystem.getSequence(new File(next_song));
  38.             this.seq.setSequence(sequence);
  39.             System.out.println("Playing");
  40.             this.seq.start();}
  41.         catch(Exception e){
  42.             System.out.println("Error!");
  43.             e.printStackTrace();
  44.             play_next_song();
  45.         }
  46.     }
  47.     public void start_playing () {
  48.         try{this.seq.open();}catch(Exception e){}
  49.         System.out.println("Starting to play");
  50.         this.play_next_song();
  51.     }
  52.     public static void play(){
  53.         Queue<String> song_queue = new LinkedList<String>();
  54.         PlayMusicInOrder.add_midi_files(song_queue,new File( "C:\\Users\\Kyle\\Desktop\\clieent\\Client\\cache\\Music\\"));
  55.         System.out.println("Songs added!");
  56.         PlayMusicInOrder player = new PlayMusicInOrder (song_queue);
  57.         player.start_playing();
  58.     };
  59.  
  60.     public static void add_midi_files(Queue<String> queue, File top_dir){
  61.         try {
  62.             for (File f: top_dir.listFiles())
  63.                 if (f.isDirectory()) {
  64.                     add_midi_files(queue, f);
  65.                 } else if (f.isFile())
  66.                 {
  67.                     System.out.println(f);
  68.                     queue.add(f.getAbsolutePath());
  69.                 }
  70.                 else
  71.                 {}
  72.         } catch (NullPointerException e) {
  73.             e.printStackTrace();
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement