Você está na página 1de 2

12/07/13 Como trabalhar com mltiplos sons em Java ME - Nokia Developer Wiki www.developer.nokia.

com/Community/Wiki/Como_trabalhar_com_mltiplos_sons_em_Java_M E 1/2 Categories: Java ME Lang-Portuguese Code Examples A maioria dos telefones ir acionar uma exceo se voc tentar manter dois sons carregad os ao mesmo tempo, por isso o truque liberar o som anterior antes de jogar o prximo. O cdigo abaixo mostra uma classe ShaderManager, que mantm o par de todos os sons em seu midlet em uma hashtable. Ele tambm mantm o controle do tempo que o ltimo som foi tocado, porque se voc tentar reproduzir dois sons muito prximos uns dos outros ele ir falhar na maioria dos telefones. import java.io.InputStream; import java.util.Enumeration; import java.util.Hashtable; import javax.microedition.media.Manager; import javax.microedition.media.Player; public class SoundManager { private static Hashtable mapSound = new Hashtable(); private static long timeLastSound = 0; private static boolean enabled = true; static public Player getPlayer(String sound) { Player player; player = (Player)mapSound.get(sound); if (player == null) { try { String fileSpec = "/"; fileSpec += sound; InputStream is = SoundManager.class.getResourceAsStream(fileSpec); if (fileSpec.endsWith("amr")) player = Manager.createPlayer(is, "audio/amr"); else if (fileSpec.endsWith("mid")) player = Manager.createPlayer(is, "audio/midi"); mapSound.put(sound, player); } catch (Exception ex) { DebugStuff.print(ex.toString()); } } return player; } public static void playSound(String sound) { if (!enabled) return; if (Math.abs(System.currentTimeMillis() - timeLastSound) < 250) { DebugStuff.print("tente executar 2 sons muito em breve"); return; } timeLastSound = System.currentTimeMillis(); Player player = getPlayer(sound); try { Enumeration enumValues; Player playerOther; for (enumValues = mapSound.elements(); enumValues.hasMoreElements() ;) {

playerOther = (Player)enumValues.nextElement(); if (playerOther != player && playerOther.getState() == Player.STARTED) playerOther.deallocate(); } if (player.getState() == Player.STARTED) { DebugStuff.print("sound already started"); return; } player.start(); } catch (Exception e) { DebugStuff.print(e.toString()); } } public static void setEnabled(boolean enabled) { SoundManager.enabled = enabled; } } Como chamar usar o ShaderManager: SoundManager.playSound("sound1.amr"); (Repare que eu estou usando o formato AMR ao invs de .wav, porque AMR muito menor , tem mais ou menos o mesmo e igualmente bem suportado como o formato WAV.) Como trabalhar com mltiplos sons em Java ME 12/07/13 Como trabalhar com mltiplos sons em Java ME - Nokia Developer Wiki www.developer.nokia.com/Community/Wiki/Como_trabalhar_com_mltiplos_sons_em_Java_M E 2/2 Retrieved from "http://www.developer.nokia.com/Community/Wiki/index.php?title=Co mo_trabalhar_com_m%C3%BAltiplos_sons_em_Java_ME&oldid=121549" This page was last modified on 8 December 2011, at 09:01. 88 page views in the last 30 days. F ea tured links D istrib utio n Co mmunity R eso urces

Você também pode gostar