public void draw() { background(0); stroke(255); // perform a forward FFT on the samples in jingle's mix buffer, // which contains the mix of both the left and right channels of the file fft.forward(jingle.mix); for (int i = 0; i < fft.specSize(); i++) { // draw the line for frequency band i, scaling it up a bit so we can see it line(i, height / 2, i, height / 2 - fft.getBand(i) * 4); } stroke(255, 0, 0); for (int beat : beats) { float x = beat * width / totalSamples; line(x, 0, x, height); } stroke(255, 255, 0); float x = jingle.position() * width / jingle.length(); line(x, 0, x, height); image(preview, 0, 0); stroke(0, 255, 0); float s = (float) width / samples.length; line(loopStart * s, 0, loopStart * s, height); line(loopEnd * s, 0, loopEnd * s, height); }
/** * Test method for {@code AudioPlayer#play(URL)} * * @throws Exception audio fault exception, e.g. can't open stream, unhandleable audio format * @throws MalformedURLException wrong URL */ @Test(timeout = 4 * MAX_DURATION) public void testPlay() throws MalformedURLException, Exception { File wav1 = new File(TestUtils.getRegressionDataFile(6851, "20111003_121226.wav")); File wav2 = new File(TestUtils.getRegressionDataFile(6851, "20111003_121557.wav")); for (File w : new File[] {wav1, wav2}) { System.out.println("Playing " + w.toPath()); URL url = w.toURI().toURL(); long start = System.currentTimeMillis(); AudioPlayer.play(url); assertTrue(AudioPlayer.playing()); assertFalse(AudioPlayer.paused()); AudioPlayer.pause(); assertFalse(AudioPlayer.playing()); assertTrue(AudioPlayer.paused()); AudioPlayer.play(url, AudioPlayer.position()); while (AudioPlayer.playing() && (System.currentTimeMillis() - start) < MAX_DURATION) { Thread.sleep(500); } long duration = System.currentTimeMillis() - start; System.out.println("Play finished after " + Utils.getDurationString(duration)); assertTrue(duration < MAX_DURATION); AudioPlayer.reset(); Thread.sleep(1000); // precaution, see #13809 } }
public void execute(int globalTime) { if (!ap.isPlaying()) { int playAt = globalTime - timeStamp; ap.play(playAt); println("gTime is " + globalTime + " delTime is " + playAt); } }
public static void main(String[] args) { AudioPlayer ap = new AudioPlayer(); ap.play("mp3", "hello"); ap.play("mkv", "hello"); ap.play("mp4", "hello"); ap.play("mp12", "hello"); }
public static void main(String[] args) { AudioPlayer audioPlayer = new AudioPlayer(); audioPlayer.play("mp3", "beyond the horizon.mp3"); audioPlayer.play("mp4", "alone.mp4"); audioPlayer.play("vlc", "far far away.vlc"); audioPlayer.play("avi", "mind me.avi"); }
private boolean executeCantico(int numCantico) { String[] arquivos = FileUtils.fileNamesOnFolder(CANTICOS_FOLDER); String fileNameExpected = CANTICOS_FILEPREFIX + String.format("%03d", numCantico) + ".mp3"; boolean foundFile = false; for (String arquivo : arquivos) { if (arquivo.equals(fileNameExpected)) { try { if (audioThread != null && audioThread.isAlive()) { audioPlayer.stop(); audioThread.interrupt(); } audioPlayer = new AudioPlayer(new File(CANTICOS_FOLDER + "\\" + arquivo)); audioThread = new Thread(audioPlayer); audioThread.start(); foundFile = true; print("executing " + fileNameExpected); break; } catch (Exception e) { e.printStackTrace(); } } } return foundFile; }
@Override public void play(String audioType, String fileName) { if (audioType.equalsIgnoreCase("avi")) { videoPlayer.playVideo(fileName); } else if (audioType.equalsIgnoreCase("mp3")) { audioPlayer.playAudio(fileName); } }
@Override public void onDestroy() { // TODO Auto-generated method stub Log.d("g54mdp", "onDestroy4"); audioPlayer.running = false; audioPlayer = null; super.onDestroy(); }
public static void main(String args[]) { AudioPlayer Core = new AudioPlayer(); Core.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Core.setSize(400, 200); Core.setLocationRelativeTo(null); Core.setResizable(false); Core.setBackground(Color.BLACK); // not working, why? Core.setVisible(true); Core.setTitle("Audio Player"); }
public void leave(Person p) { original(p); if (weight <= MAX_WEIGHT) { alarm.stop(as); System.out.println("Start [LoadWeigthByPass]"); LBPOkToGo = true; blockEnter = false; } }
public void enter(int floor, Person p) { original(floor, p); if (weight > MAX_WEIGHT) { System.out.println("Stop [LoadWeigthByPass]"); LBPOkToGo = false; blockEnter = true; alarm.start(as); } }
private static void lerArquivoDoDisco() { // TODO Auto-generated method stub // TODO Auto-generated method stub JFileChooser fc = new JFileChooser(); fc.showOpenDialog(null); File file = fc.getSelectedFile(); AudioPlayer player = null; Thread th = null; try { String ext = fc.getTypeDescription(file); if (ext.toLowerCase().contains("mp3")) { player = new AudioPlayer(file); // player.play(); th = new Thread(player); th.start(); } } catch (Exception e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); } Scanner sc = new Scanner(System.in); while (true) { String input = sc.nextLine(); if (input.equals("fechar")) { player.stop(); th.interrupt(); System.exit(0); } if (input.equals("pausar")) { player.pause(); } if (input.equals("play")) { try { player.resume(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public void hearAudio() { View view = new View(this); AudioPlayer audio = new AudioPlayer(view); Uri uri = Uri.parse(advice); try { audio.setAudioUri(uri); } catch (IOException e) { } ViewGroup.LayoutParams params = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); view.setLayoutParams(params); layout.addView(view); audio.start(); }
/** * Attempts to play this AudioClip, the method returns immediately and never throws exceptions. If * playing fails, it fails silently. */ public synchronized void play() { if (successfulPlayer != null) { try { successfulPlayer.play(this); return; } catch (Exception e) { } // Ignore any exceptions } String cmdSpecifiedPlayer; try { cmdSpecifiedPlayer = System.getProperty("free.util.audio.player"); } catch (SecurityException e) { cmdSpecifiedPlayer = null; } if (cmdSpecifiedPlayer != null) { try { Class playerClass = Class.forName(cmdSpecifiedPlayer); AudioPlayer player = (AudioPlayer) playerClass.newInstance(); if (!player.isSupported()) { System.err.println( cmdSpecifiedPlayer + " is not supported on your system - audio disabled."); successfulPlayer = new NullAudioPlayer(); return; } player.play(this); successfulPlayer = player; } catch (Throwable e) { if (e instanceof ThreadDeath) throw (ThreadDeath) e; System.err.println(cmdSpecifiedPlayer + " failed - audio disabled."); successfulPlayer = new NullAudioPlayer(); } } else { for (int i = 0; i < PLAYER_CLASSNAMES.length; i++) { String classname = PLAYER_CLASSNAMES[i]; try { Class playerClass = Class.forName(classname); AudioPlayer player = (AudioPlayer) playerClass.newInstance(); if (!player.isSupported()) continue; player.play(this); System.err.println("Will now use " + classname + " to play audio clips."); successfulPlayer = player; } catch (Throwable e) { if (e instanceof ThreadDeath) throw (ThreadDeath) e; continue; } break; } if (successfulPlayer == null) { System.err.println("All supported players failed - audio disabled"); successfulPlayer = new NullAudioPlayer(); } } }
public void analyze(int start) { diffs.clear(); loopStart = start; int SUB = 64; int N = 2048 * SUB; float maxDiff = 0; int upper = (int) min(samples.length - N, start + 30 * jingle.getFormat().getSampleRate()); for (int i = start + N; i < upper - N; i++) { if (i % 10000 == 0) System.out.println(i + "/" + (upper - start)); float diff = 0; for (int j = 0; j < N; j += SUB) { diff += Math.abs(samples[i + j] - samples[start + j]); } maxDiff = max(maxDiff, diff); diffs.put(i, new PVector(i, diff)); } preview.beginDraw(); preview.clear(); preview.noFill(); preview.stroke(255); preview.beginShape(); for (int i = 0; i <= width; i++) { preview.vertex( i, preview.height / 2 + height / 2 * samples[(int) (i * (samples.length - 1l) / width)]); } preview.endShape(); preview.endDraw(); preview.beginDraw(); preview.clear(); preview.stroke(255, 0, 0); preview.beginShape(); int n = 0; int prevX = 0; float sum = maxDiff; for (PVector diff : diffs.values()) { int x = (int) (diff.x * (long) width / samples.length); sum = min(diff.y, sum); n = 1; if (x != prevX) { preview.vertex(x, preview.height * sum / maxDiff / n); n = 0; sum = maxDiff; prevX = x; } } preview.endShape(); preview.endDraw(); }
@Override public void keyPressed() { if (key == 'r') { loopEnd = (int) (mouseX * (long) samples.length / width); } if (key == 'a') { PVector best = new PVector(0, Float.MAX_VALUE); int sampleNr = (int) (mouseX * (long) samples.length / width); for (int i = sampleNr - 500000; i < sampleNr + 500000; i++) { PVector diff = diffs.get(i); if (diff != null) { if (diff.y < best.y) best = diff; } } if (best.x != 0) { loopEnd = (int) best.x; } } if (keyCode == RIGHT) { jingle.cue((int) (loopEnd * 1000l / jingle.getFormat().getSampleRate() - 1000)); } }
// Make sure only one direction is going at once void setMode(DeviceMode newMode) { if (mode == newMode) return; switch (mode) { case MODE_PLAY: audioPlayer.closeLine(); break; case MODE_RECORD: audioRecorder.closeLine(); break; } mode = newMode; }
public static void music(char gameResult) { AudioPlayer MGP = AudioPlayer.player; AudioStream BGM, BGM1; AudioData MD; ContinuousAudioDataStream loop = null; try { InputStream winAudio = new FileInputStream("Audio/winrevised.wav"); InputStream loseAudio = new FileInputStream("Audio/loserevised.wav"); BGM = new AudioStream(winAudio); BGM1 = new AudioStream(loseAudio); if (gameResult == 'w') AudioPlayer.player.start(BGM); else if (gameResult == 'l') AudioPlayer.player.start(BGM1); // MD = BGM.getData(); // loop = new ContinuousAudioDataStream(MD); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException error) { error.printStackTrace(); } MGP.start(loop); }
@Override public void execute() { myAudio.stop(); }
public static void main(String[] args) { AudioPlayer ap = new AudioPlayer(); ap.play("mp3", "hello"); ap.play("mkv", "world"); ap.play("xml", "pointdata"); }
@Override public void execute() { myAudio.rewind(); }
public void stop() { dec.stop(); player.stop(); jb.reset(); }
public void setPreferredSourceMixer(String minfo) { audioPlayer.setPreferredMixer(minfo); }
public void setSpeexAEC(boolean on) { player.setSpeexAEC(on); }
public int supportedPlaySampleRate(int desiredSampleRate) throws LineUnavailableException { return audioPlayer.supportedSampleRate(desiredSampleRate); }
public Mixer.Info getPreferredSourceMixer() { return audioPlayer.getPreferredMixer(); }
public void start() { dec.start(); player.start(); Log.d(TAG, "Started"); }
/** * Set the preferred source (output) mixer to use. * * @param mInfo Description of mixer */ public void setPreferredSourceMixer(Mixer.Info mInfo) { audioPlayer.setPreferredMixer(mInfo); }
public void setup() { size(1200, 200, JAVA2D); preview = createGraphics(width, 100); minim = new Minim(this); mimp = new JSMinim(this); mimp.debugOn(); // specify that we want the audio buffers of the AudioPlayer // to be 1024 samples long because our FFT needs to have // a power-of-two buffer size and this is a good size. jingle = minim.loadFile(filename, 1024); // loop the file indefinitely jingle.loop(); // create an FFT object that has a time-domain buffer // the same size as jingle's sample buffer // note that this needs to be a power of two // and that it means the size of the spectrum will be half as large. fft = new FFT(jingle.bufferSize(), jingle.sampleRate()); MultiChannelBuffer sampleBuffer = new MultiChannelBuffer(0, 0); // params don't matter! float jingleBuffer = loadFileIntoBuffer(filename, sampleBuffer); totalSamples = sampleBuffer.getBufferSize(); beat = new BeatDetect(4096 * 4, jingle.sampleRate()); beat.setSensitivity(1); beat.detectMode(BeatDetect.FREQ_ENERGY); // find ALL beats float data[] = new float[2048]; samples = sampleBuffer.getChannel(1); // for( int i = 0; i < samples.length; i+= data.length ){ // int j = Math.min( samples.length-1, i + data.length - 1 ); //// System.out.println( "copy up to including " + ( j ) + " / " + samples.length); // System.arraycopy( samples, i, data, 0, 1 + j - i ); // beat.detect( data ); // if( beat.isOnset() ){ // beats.add( i ); // System.out.println( i + ": " + beat.isKick() ); // } // } // pick some starting point ... loopEnd = samples.length; int start = (int) random(3 * samples.length / 5); analyze(start); new Thread() { public void run() { try { while (true) { Thread.sleep(1); // figure it out int s = (int) (loopStart * 1000l / jingle.getFormat().getSampleRate()); int e = (int) (loopEnd * 1000l / jingle.getFormat().getSampleRate()); if (jingle.position() < s) jingle.cue(s + 10); else if (jingle.position() > e) jingle.cue(s); else continue; Thread.sleep(500); } } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); // System.exit(0); }
/** * Prompt the user for input and interpret the command recieved. * * @throws MidiUnavailableException, InvalidMidiDataException */ public static void executeUserCommand() throws MidiUnavailableException, InvalidMidiDataException { while (true) { System.out.println(); System.out.print("Enter User Command. (Type 'quit' to quit) "); System.out.println(); InputStreamReader isr = new java.io.InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); try { String userInput = br.readLine(); // the userInput is evaluated to different functions encased in // the following if blocks. // some functions take in user input and evaluates what the user // wants by separating it into different sections if (userInput.equals("quit")) { Execution.quit(player); Execution.writePlaylistsToFile(library); break; } else if (userInput.startsWith("create playlist") && userInput.endsWith(")")) { library = Execution.createPlaylist(userInput, library); Execution.writePlaylistsToFile(library); } else if (userInput.startsWith("play playlist")) { String[] userInputSplited = userInput.split(" "); String playlist = userInputSplited[2]; if (userInputSplited.length == 4 & userInput.matches(".*\\d.*")) { // When input length is 4, then play specified song that // user inputs int songIndex; try { songIndex = Integer.parseInt(userInputSplited[3]) - 1; } catch (NumberFormatException nfe) { System.out.println( "Please enter a valid index that is in this playlist. Please try again."); continue; } try { player = Execution.playPlaylistSong(library, player, playlist, songIndex); } catch (IndexOutOfBoundsException iobe) { System.out.println( "Please enter a valid index that is in this playlist. Please try again."); continue; } catch (NullPointerException e) { System.out.println("Playlist does not exist. Please try again."); continue; } } else if (userInputSplited.length == 3 | (!userInput.matches(".*\\d.*"))) { // if command does not contain integer, play entire // playlist Playlist pl; pl = library.findPlaylist(playlist); // checks if playlist exists if (pl == null) { System.out.println("Playlist does not exist. Please try again."); continue; } for (int i = 0; i < pl.numberOfSongs(); i++) { // plays the playlist songs one by one try { player = Execution.playPlaylistSong(library, player, pl, i); } catch (IOException e) { System.out.println("Midi file not available!"); } player.checkIfRunning(); } } } else if (userInput.startsWith("delete playlist")) { library = Execution.deletePlaylist(library, userInput); } else if (userInput.startsWith("find songs by")) { library = Execution.findSongByArtist(library, userInput); } else if (userInput.startsWith("play song")) { String[] userInputSplited = userInput.split(" "); int songIndex; try { songIndex = Integer.parseInt(userInputSplited[2]) - 1; } catch (NumberFormatException nfe) { // the user input can only be an integer System.out.println("Please enter a song index!"); continue; } try { player = Execution.playSong(library, player, songIndex); } catch (IndexOutOfBoundsException iobe) { System.out.println("Song does not exist, please try again."); continue; } } else if (userInput.startsWith("delete song")) { String[] userInputSplited = userInput.split(" "); int songIndex; System.out.println( "Are you sure you want to permanently remove this song?(yes to confirm, type anything to return to menu)"); String confirm = br.readLine().trim(); // additional layer of confirmation, in case of accidental // deletion if (confirm.equals("yes")) { try { songIndex = Integer.parseInt(userInputSplited[2]) - 1; } catch (NumberFormatException nfe) { System.out.println("Please enter a song index!"); continue; } try { library.delSong(songIndex); library = Execution.readPlaylistXml(library); library.displayAllLibrarySongs(); library.displayAllLibraryPlaylists(); } catch (IndexOutOfBoundsException iobe) { System.out.println("Song does not exist, please try again."); continue; } } } else if (userInput.startsWith("play random")) { // User may enter any of the three commands that begin with // play random // each is encased in an if block List<Integer> indexes; if (userInput.equals("play random for all")) { int numberOfSongs = library.numberOfSongs(); indexes = new ArrayList<Integer>(); for (int i = 0; i < numberOfSongs; i++) { indexes.add(i); } player = Execution.playRandom(library, player, indexes); } else if (userInput.startsWith("play random") & userInput.endsWith(")")) { // When user wants to play specified songs randomly String[] userInputSplited = userInput.split(" "); userInputSplited = userInputSplited[2].substring(1, userInputSplited[2].length() - 1).split(","); ArrayList<String> tempList = new ArrayList<String>(Arrays.asList(userInputSplited)); indexes = new ArrayList<Integer>(tempList.size()); for (String i : tempList) { if (Integer.valueOf(i) > library.numberOfSongs()) { System.out.println( "the song of index " + Integer.valueOf(i) + " does not exist in your music libaray."); } else { indexes.add(Integer.valueOf(i) - 1); } } player = Execution.playRandom(library, player, indexes); } else if (userInput.startsWith("play random playlist")) { String[] userInputSplited = userInput.split(" "); // The 4th element in list is the playlist that user // wants to play randomly String playlist = userInputSplited[3]; Playlist pl = library.findPlaylist(playlist); try { indexes = new ArrayList<Integer>(pl.numberOfSongs()); } catch (NullPointerException npe) { System.out.println("Playlist " + playlist + " does not exist. Please try again."); continue; } for (int n = 0; n < pl.numberOfSongs(); n++) { indexes.add(n); } player = Execution.playRandom(library, player, pl, indexes); } } else if (userInput.startsWith("download")) { String title = userInput.split("download ")[1]; library.downloadSong(title); } else if (userInput.startsWith("display jcloud songs")) { library.displayJcloudSongs(); } else if (userInput.startsWith("get downloaded songs")) { library.downloadPreviousSongs(); library.displayAllLibrarySongs(); } else if (userInput.equals("display songs")) { library.displayAllLibrarySongs(); } else if (userInput.equals("display library")) { library.displayAllLibrarySongs(); library.displayAllLibraryPlaylists(); } else if (userInput.equals("display playlists")) { library.displayAllLibraryPlaylists(); // Shows nothing when no playlist have been created } else if (userInput.equals("help")) { displayUserManual(); } else if (userInput.contains("pause")) { try { player.pause(); } catch (NullPointerException e) { continue; } } else if (userInput.contains("resume")) { try { player.resume(); } catch (NullPointerException e) { continue; } } else { System.out.println( "Invalid command. Please pay attention to input format and try again!"); } } catch (IOException e) { e.printStackTrace(); } } }