Advertisement
Guest User

eXCEPTION

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