| Reply | « Previous Thread | Next Thread » |
|
HI
Can any one help me out with this problem???? i am successfully running the wav files using ResourceAsStream() in a static applciation but when i am trying to read the same wav file from the site i am getting the total size but its not playing i am only getting tick sound. whats the reason??? can any one help me out with a good solution.... please see the code below and try to help me out. Thanks in advance import java.lang.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*; import java.util.*; import com.nokia.mid.sound.*; public class Test extends MIDlet implements Runnable,CommandListener { private Display display; private Command cmdStart; private Form frm = null; //private DisplayCanvas dCanvas = null; private static Test instance = null; static Form f = null; static byte[] tone = null; public Test() { display = Display.getDisplay(this); cmdStart = new Command("Start",Command.SCREEN,2); instance = this; f = new Form("test"); f.append("Testing Sound"); f.addCommand(cmdStart); f.setCommandListener(this); } public void startApp() throws MIDletStateChangeException { // sImage(); display.setCurrent(f); } public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void commandAction(Command cmd, Displayable d) { try{ if(cmd == cmdStart) { (new Thread(this)).start(); } }catch(Exception e){ System.out.println("this is in comand action"); System.out.println(e.getMessage()); } } public void run() { tone = previewTone("raju") ;//from url or site //tone = loadFile("raju.wav") ;//from resource System.out.println("length is "+tone.length); Sound sd = new Sound(tone, 5); sd.init(tone, 5); sd.play(0); } public byte[] previewTone(String song) { byte[] b = null; StringBuffer buffer=null; HttpConnection c = null; InputStream is = null; try{ buffer = new StringBuffer(); c = (HttpConnection)Connector.open("http://203.196.161.72/audio/"+song.trim()+".wav"); is = c.openInputStream(); System.out.println("content length is :"+c.getLength()); b = new byte[(int)c.getLength()]; is.read(b); System.out.println("content generation for file "+song+" is completed"); }catch(Exception e){ e.printStackTrace(); } finally{ try{ is.close(); c.close(); }catch(Exception e){ e.printStackTrace(); } } return b; } private byte[] loadFile(String name) { try { System.out.println("in load file name "+name); InputStream is = getClass().getResourceAsStream(name.trim()); System.out.println("in after load file"); StringBuffer str = new StringBuffer(); byte b[] = new byte[1]; System.out.println("in after load file111111"); while ( is.read(b) != -1 ) { str.append(new String(b)); } System.out.println("in after load file222222222"); is.close(); byte[] binaryData = new byte[str.length()]; for (int i = 0; i < binaryData.length; ++i) { binaryData[i] = (byte)str.charAt(i); } return binaryData; } catch (Exception e) { //e.printStackTrace(); System.out.println("exception in loading the wave file "+e.getMessage()); } return null; } } |
|
I wonder if the input stream you're reading from is still positioned before all the HTTP header stuff.
Try spitting out the first 50 or so bytes and see if they're like "HTTP 1.1 OK" and stuff. If that's your problem, then you just gotta advance the input stream until you get to the sequence "/r/n/r/n", then build the byte[] after that. |
|
Hi
atlast using midp1.0 nokia sound api i am able to play the wav file of size 90 kb over the network also(http). try the code below......cheers import java.lang.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*; import java.util.*; import com.nokia.mid.sound.*; public class Test extends MIDlet implements Runnable,CommandListener { private Display display; private Command cmdStart; private Form frm = null; //private DisplayCanvas dCanvas = null; // private static Test instance = null; static Form f = null; static byte[] tone = null; // private static int [] formats ; public Test() { display = Display.getDisplay(this); cmdStart = new Command("Start",Command.SCREEN,2); // instance = this; f = new Form("test"); f.append("Testing Sound"); f.addCommand(cmdStart); f.setCommandListener(this); } public void startApp() throws MIDletStateChangeException { // sImage(); display.setCurrent(f); } public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void commandAction(Command cmd, Displayable d) { try{ if(cmd == cmdStart) { (new Thread(this)).start(); } }catch(Exception e){ System.out.println("this is in comand action"); System.out.println(e.getMessage()); } } public void run() { //formats = Sound.getSupportedFormats(); tone = previewTone("womanssong.wav") ; //tone = loadFile("bark.wav") ; System.out.println("length is "+tone.length); Sound sd = new Sound(tone, Sound.FORMAT_WAV); sd.init(tone, 5); sd.play(0); } public byte[] previewTone(String song) { // byte[] b = null; StringBuffer buffer=null; HttpConnection c = null; InputStream is = null; try{ buffer = new StringBuffer(); c = (HttpConnection)Connector.open("http://localhost/audio/"+song.trim()); is = c.openInputStream(); System.out.println("content length is :"+c.getLength()); /* b = new byte[(int)c.getLength()]; is.read(b); System.out.println("content generation for file "+song+" is completed"); */ StringBuffer str = new StringBuffer(); byte b[] = new byte[1]; System.out.println("in after load file111111"); while ( is.read(b) != -1 ) { str.append(new String(b)); } System.out.println("in after load file222222222"); is.close(); byte[] binaryData = new byte[str.length()]; for (int i = 0; i < binaryData.length; ++i) { binaryData[i] = (byte)str.charAt(i); } return binaryData; }catch(Exception e){ e.printStackTrace(); } finally{ try{ is.close(); c.close(); }catch(Exception e){ e.printStackTrace(); } } //return b; return null; } } |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|