You Are Here:

Community: Developer Discussion Boards

#1 Old Playing wav file connecting to server(url) - 2003-07-09, 09:33

Join Date: Jul 2003
Posts: 1
valuelabs
Offline
Registered User
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;
}




}
Reply With Quote

#2 Old Maybe need to strip HTTP header stuff? - 2003-07-10, 01:17

Join Date: Jul 2003
Posts: 4
elovy
Offline
Registered User
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.
Reply With Quote

#3 Old got solution - 2003-07-10, 11:51

Join Date: Mar 2003
Posts: 181
Location: Hyderabad
Send a message via Yahoo to j2me_raj
j2me_raj
Offline
Regular Contributor
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 With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d18645X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZentertainmentQ qfnZtopicQUqfnTopicZj2meQ qfnZtopicQUqfnTopicZjavaQ qfnZtopicQUqfnTopicZmediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ