@ActionDoc(text = "plays a sound from the sounds folder") public static void playSound( @ParamDoc(name = "filename", text = "the filename with extension") String filename) { try { InputStream is = new FileInputStream(SOUND_DIR + File.separator + filename); if (filename.toLowerCase().endsWith(".mp3")) { Player player = new Player(is); playInThread(player); } else { AudioInputStream ais = AudioSystem.getAudioInputStream(is); Clip clip = AudioSystem.getClip(); clip.open(ais); playInThread(clip); } } catch (FileNotFoundException e) { logger.error("Cannot play sound '{}': {}", new String[] {filename, e.getMessage()}); } catch (JavaLayerException e) { logger.error("Cannot play sound '{}': {}", new String[] {filename, e.getMessage()}); } catch (UnsupportedAudioFileException e) { logger.error( "Format of sound file '{}' is not supported: {}", new String[] {filename, e.getMessage()}); } catch (IOException e) { logger.error("Cannot play sound '{}': {}", new String[] {filename, e.getMessage()}); } catch (LineUnavailableException e) { logger.error("Cannot play sound '{}': {}", new String[] {filename, e.getMessage()}); } }
/** * This function converts .mp3 song to .WAV song * * @param filepathToConvert - the source file path (mp3) * @param convertedFilepath - the destination file path (wave) */ public void convertMP3ToWav(String filepathToConvert, String convertedFilepath) { try { converter.convert(filepathToConvert, convertedFilepath); } catch (JavaLayerException e) { e.printStackTrace(); } }
@Override public int read(ByteBuffer buf) throws IOException { ShortBuffer sbuf = buf.asShortBuffer(); int total = 0; while (sbuf.hasRemaining() && _header != null) { if (_buffer == null) { try { _buffer = (SampleBuffer) _decoder.decodeFrame(_header, _istream); _istream.closeFrame(); _header = _istream.readFrame(); } catch (JavaLayerException e) { throw new IOException(e.toString()); } } int blen = _buffer.getBufferLength(), length = Math.min(sbuf.remaining(), blen - _offset); sbuf.put(_buffer.getBuffer(), _offset, length); if ((_offset += length) >= blen) { _offset = 0; _buffer = null; } total += (length * 2); } buf.position(buf.position() + total); return total; }
@Override public void init(InputStream in) throws IOException { _istream = new Bitstream(in); try { _header = _istream.readFrame(); } catch (JavaLayerException e) { throw new IOException(e.toString()); } _decoder = new Decoder(); }
/** * Starts this applet. An input stream and audio device are created and passed to the play() * method. */ public void start() { String name = getAudioFileName(); try { InputStream in = getAudioStream(); AudioDevice dev = getAudioDevice(); play(in, dev); } catch (JavaLayerException ex) { synchronized (System.err) { System.err.println("Unable to play " + name); ex.printStackTrace(System.err); } } }
public void songManager() { UI ui = new UI(); int choice = ui.getChoice(); Play p = new Play(this); while (choice != 5) { switch (choice) { case 1: sortByArtist(); print(); break; case 2: sortByTitle(); print(); break; case 3: Scanner sc = new Scanner(new InputStreamReader(System.in)); int songChoice = 0; while (songChoice < 1 || songChoice > size()) { System.out.println("Enter Song Choice: "); sortByArtist(); print(); songChoice = sc.nextInt(); } try { p.playSong(songChoice, this); } catch (JavaLayerException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case 4: if (player != null) { player.close(); } break; default: break; } choice = ui.getChoice(); if (choice == 5) break; } }
public static void play(final String audioFilePath) { if (isEnabled) { try { final AdvancedPlayer p = new AdvancedPlayer(new FileInputStream(new File(audioFilePath))); Thread t = new Thread() { @Override public void run() { try { p.play(); } catch (JavaLayerException e) { e.printStackTrace(); } } }; t.setPriority(Thread.MIN_PRIORITY); t.start(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (JavaLayerException e) { e.printStackTrace(); } } }
@ActionDoc(text = "plays an audio stream from an url") public static synchronized void playStream( @ParamDoc(name = "url", text = "the url of the audio stream") String url) { if (streamPlayer != null) { // if we are already playing a stream, stop it first streamPlayer.close(); streamPlayer = null; } if (url == null) { // the call was only for stopping the currently playing stream return; } try { if (url.toLowerCase().endsWith(".m3u")) { InputStream is = new URL(url).openStream(); String urls = IOUtils.toString(is); for (String line : urls.split("\n")) { if (!line.isEmpty() && !line.startsWith("#")) { url = line; break; } } } else if (url.toLowerCase().endsWith(".pls")) { InputStream is = new URL(url).openStream(); String urls = IOUtils.toString(is); for (String line : urls.split("\n")) { if (!line.isEmpty() && line.startsWith("File")) { Matcher matcher = plsStreamPattern.matcher(line); if (matcher.find()) { url = matcher.group(1); break; } } } } URL streamUrl = new URL(url); URLConnection connection = streamUrl.openConnection(); InputStream is = null; if (connection.getContentType().equals("unknown/unknown")) { // Java does not parse non-standard headers used by SHOUTCast int port = streamUrl.getPort() > 0 ? streamUrl.getPort() : 80; // Manipulate User-Agent to receive a stream shoutCastSocket = new Socket(streamUrl.getHost(), port); OutputStream os = shoutCastSocket.getOutputStream(); String user_agent = "WinampMPEG/5.09"; String req = "GET / HTTP/1.0\r\nuser-agent: " + user_agent + "\r\nIcy-MetaData: 1\r\nConnection: keep-alive\r\n\r\n"; os.write(req.getBytes()); is = shoutCastSocket.getInputStream(); } else { is = streamUrl.openStream(); } if (is != null) { Player player = new Player(is); streamPlayer = player; playInThread(player); } } catch (JavaLayerException e) { logger.error("Cannot play stream '{}': JavaLayerException - {}", url, e.getMessage()); } catch (MalformedURLException e) { logger.error("Cannot play stream '{}': MalformedURLException - {}", url, e.getMessage()); } catch (IOException e) { logger.error("Cannot play stream '{}': {}", url, e); } }
@Override public void init(GameContainer gc) { try { Scanner test = ResourceLoader.openFile("Test"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { ResourceLoader.createTextbox("test"); } catch (TextboxException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { theTileMap = new BasicTileMap("res/testMap.tmx"); // ((BasicTileMap)(theTileMap)).setPathing(tileMapPathing); theTileMap.useDefaultCamera(); System.out.println( "LEFT: " + theTileMap.getLeftBound() + " RIGHT: " + theTileMap.getRightBound() + " UPPER: " + theTileMap.getUpperBound() + " LOWER: " + theTileMap.getLowerBound()); theTileMap.setViewCollision(true); Game.setGameMap(theTileMap); } catch (SlickException e) { e.printStackTrace(); } player1 = new TestPlayer(100, 200, 50, 50); player1.setSpeed(5f); player1.setShape("Rectangle"); player1.setMovableCollision(true); player1.setViewCollision(false); pawnCamera = new Camera(player1, 100, 50); player1.attachCamera(pawnCamera); player2 = new Pawn(300, 400, 50, 50); player2.setShape("Rectangle"); player2.setMovableCollision(false); player2.useDefaultCamera(); // player3 = new Player(350, 600, 100, 100); // player3.useDefaultCamera(); Game.setGameCamera(player1.getCamera()); testBox2 = new BasicDialogbox(150, 100, 300, 300); testBox2.load("testDialog.txt"); testBox2.setUseCameraCoordinates(true); testBox2.addTextboxListener( new TextboxListener() { @Override public void textboxStopped(TextboxEvent e) { testBox.stop(); } }); // pathFinder.run testBox = new Confirmbox(75, 300, 500, 200); testBox.load("testConfirmDialog.txt"); testBox.setColumns(2); testBox.drawColumnLines(true); Textbox.setTextboxScrollUpKey(Input.KEY_W); Textbox.setTextboxScrollDownKey(Input.KEY_S); Textbox.setTextboxScrollLeftKey(Input.KEY_A); Textbox.setTextboxScrolRightKey(Input.KEY_D); testBox.pack(); testBox.setUseCameraCoordinates(true); Textbox.setTextboxActionKey(Input.KEY_SPACE); testBox.addConfirmboxListener( new ConfirmboxListener() { @Override public void textboxStopped(TextboxEvent e) {} @Override public void selectionCommandPerformed(ConfirmboxEvent e) { System.out.println(e.getSelectionCommand()); Textbox.setTextboxActionKey(Input.KEY_ENTER); testBox.pause(); testBox.setSelectionBoxActive(false); testBox.setFocused(false); testBox2.setFocused(true); if (!testBox2.hasStarted()) testBox2.start(); else { testBox2.resume(); testBox2.show(); } } public void scrollCommandPerformed(ConfirmboxEvent e) { System.out.println(e.getScrollCommand()); System.out.println(e.getSelectionCommand()); } }); gameTimer = new GameTimer(300, 300); // gameTimer.setUseCameraCoordinates(false); gameTimer.setTime(5, FPS); gameTimer.setRecurring(true); // gameTimer.setFormat("%2d:%02d:%-2d"); gameTimer.addTimerListener( new TimerListener() { public void timerReachedZero(TimerEvent e) { System.out.println("Game Timer Reached Zero"); } }); // gameTimer.start(); realTimer = new RealTimer(300, 300); realTimer.addTimerListener( new TimerListener() { @Override public void timerReachedZero(TimerEvent e) { System.out.println("Real Timer reached Zero"); } }); realTimer.setTime(20, FPS); realTimer.setRecurring(true); realTimer.start(); // realTimer.pause(); gameEntities.add(theTileMap); gameEntities.add(player1); gameEntities.add(player2); // gameEntities.add(player3); gameEntities.add(testBox); gameEntities.add(testBox2); // gameEntities.add(gameTimer); gameEntities.add(realTimer); KeyInput.addInputCommand("Move+X", Input.KEY_RIGHT, 0); KeyInput.addInputCommand("Move-X", Input.KEY_LEFT, 0); KeyInput.addInputCommand("Move+Y", Input.KEY_DOWN, 0); KeyInput.addInputCommand("Move-Y", Input.KEY_UP, 0); KeyInput.addInputCommand("PauseTextbox", Input.KEY_P, 0); KeyInput.addInputCommand("ResumeTextbox", Input.KEY_R, 0); KeyInput.addInputCommand("StopTextbox", Input.KEY_ESCAPE, 0); KeyInput.addInputCommand("Start Dialog", Input.KEY_ENTER, 0); MouseInput.addMouseInputCommand("Attack", Input.MOUSE_LEFT_BUTTON, 0); // BackgroundMusic.addSongToQueue("01.-title.mp3"); BackgroundMusic.addSongToQueue("08.-bonus-level.mp3"); try { BackgroundMusic.playAllInQueue(); } catch (JavaLayerException e) { e.printStackTrace(); } }