BowlerKernel
SequencerWAV.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.dyio.sequencer;
2 import java.io.File;
3 import com.neuronrobotics.sdk.util.ThreadUtil;
4 import javax.sound.sampled.AudioInputStream;
5 import javax.sound.sampled.AudioSystem;
6 import javax.sound.sampled.Clip;
10 public class SequencerWAV {
11 
13  private String fn="";
14 
16  // constructor that takes the name of an MP3 file
17  private Clip player;
18 
20  private int trackLength = 37;
21 
22  public SequencerWAV(String filename) {
23  fn = filename;
24  try {
25  AudioInputStream audioStream = AudioSystem.getAudioInputStream(new File(fn));
26 
27  player = AudioSystem.getClip();
28 
29  trackLength = (int) (((double)player.getMicrosecondLength())/1000.0);
30 
31  }
32  catch (Exception e) {
33  System.out.println("Problem playing file " + filename+"\r\n");
34  //e.printStackTrace();
35  throw new RuntimeException(e);
36  }
37  }
38 
42  public void pause(){
43  player.stop();
44  }
45 
49  public void close() {
50  if (player != null)
51  player.stop();
52  }
53 
59  public boolean isPlaying() {
60  if(player!=null)
61  return (player.isRunning());
62  return false;
63  }
64 
70  public int getCurrentTime() {
71  return (int) (((double) player.getMicrosecondPosition())/1000.0);
72  }
73 
79  public void setCurrentTime(int time) {
80  player.setMicrosecondPosition(time*1000);;
81  }
82 
88  public int getTrackLength(){
89  return trackLength;
90  }
91 
97  private double getPercent() {
98  if(!isPlaying()){
99  return 0;
100  }
101  if(player!=null) {
102  double pos =((double) player.getMicrosecondPosition())/1000.0;
103  double len =((double) player.getMicrosecondLength())/1000.0;
104  double percent = pos/len*100.0;
105  return percent;
106  }
107  return 0;
108  }
109 
110 
114  public void playStep(){
115 
116  player.start();
117 
118  }
119 
123  // play the MP3 file to the sound card
124  public void play() {
125 
126  player.start();
127 
128 
129  }
130 
131 
137  // test client
138  public static void main(String[] args) {
139  SequencerWAV mp3 = new SequencerWAV("track.mp3");
140 
141  mp3.play();
142  System.out.println("Track length= "+mp3.getTrackLength());
143  while(mp3.isPlaying() ){
144  System.out.println("Current "+mp3.getCurrentTime() +" Percent = "+mp3.getPercent());
145  ThreadUtil.wait(100);
146  }
147  System.out.println("Finished "+mp3.getCurrentTime()+" of "+mp3.getTrackLength());
148  System.exit(0);
149  //mediaPlayer.
150 
151  }
152 
153 
154 
155 
156 
157 }