import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import javax.microedition.io.file.FileConnection; import javax.microedition.io.file.FileSystemRegistry; import javax.microedition.media.Manager; import javax.microedition.media.PlayerListener; /** * * @author HoldFast */ public class Player implements PlayerListener { private FileConnection fConn; private OutputStream outputStream; private javax.microedition.media.Player player; private long startTime; private final Enumeration e; public String disk = ""; private boolean stop = true; private int buffer; public Player() { e = FileSystemRegistry.listRoots(); while (e.hasMoreElements()) { disk = (String) e.nextElement(); } System.out.println(disk); } public void play(final int idTrack) throws IOException { fConn = (FileConnection) Connector.open("file:///" + disk + Main.search.id.elementAt(idTrack) + ".mp3"); if (fConn.exists()) { fConn.delete(); } fConn.create(); outputStream = fConn.openOutputStream(); HttpConnection conn = (HttpConnection) Connector.open("http://music.zakon.kz/mp3.php?id=" + Main.search.id.elementAt(idTrack)); final InputStream is = conn.openInputStream(); new Thread(new Runnable() { public void run() { try { int int_1 = 512; byte[] bytes_1 = new byte[int_1]; int int_2 = 0; buffer = 0; do { int_2 = is.read(bytes_1, 0, int_1); if (int_2 != -1) { outputStream.write(bytes_1, 0, int_2); buffer += int_2; if (buffer >= 1024 * 150) { buffer = 0; if (stop) { stop = false; player = Manager.createPlayer("file:///" + disk + Main.search.id.elementAt(idTrack) + ".mp3"); player.addPlayerListener(Main.player); player.realize(); player.setMediaTime(startTime); player.start(); } } } } while (int_2 != -1); outputStream.close(); } catch (Exception ex) { } } }).start(); } public void playerUpdate(javax.microedition.media.Player player, String event, Object eventData) { if (event == null ? PlayerListener.END_OF_MEDIA == null : event.equals(PlayerListener.END_OF_MEDIA)) { startTime = player.getMediaTime(); stop = true; player.close(); } } }