1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   |  
public class Sound {
             private String name, type;
	private byte []data;
 
	public Sound(String name, byte []data, String type) {
		this.data = data;
		this.name = name;
		this.type = type;
	}
 
             public boolean load() {
		if (name.equals("")) return false;
 
		try {
			InputStream in = getClass().getResourceAsStream(name);
			if (in==null) return false;
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			byte []byteToRead = new byte[1024];
 
			while (in.read(byteToRead) > 0)
                out.write(byteToRead, 0, byteToRead.length);						
 
			this.data = out.toByteArray();
			in.close();			
			out.close();
		}
		catch (IOException e) {
			e.printStackTrace();
			return false;
		}
 
		return true;
	}
 
             public static void main(String []args) {
		Sound unSon = new Sound("/s0.mid", null, "audio/midi");
		if (unSon.load()) {
			try {
                                            if (unSon.isLoaded()) {
                                                 javax.microedition.media.Player myPlayer = javax.microedition.media.Manager.createPlayer(new ByteArrayInputStream(unSon.getData()),unSon.getType());
 
                                                 myPlayer.realize();
                                                 myPlayer.prefetch();
                                                 myPlayer.start();
                                            }				
			}
			catch (Exception e) {
                                            e.printStackTrace();
                                      }
 
		}
		else System.out.println("Le son n'existe pas");
	}
} | 
Partager