Audio Streaming OutOfMemoryError - java-me

I am trying to stream an audio content (AAC) from server. The player starts without any problem, and after some time throws OutOfMemoryError. I think I need to buffer the audio. Any one please guide me to implement (or get rid-of this error) buffering.
Here is my code.
`HttpConnection c = null;
InputStream is = null;
try {
c = (HttpConnection) Connector.open(streamURL,
Connector.READ_WRITE);
int rc = c.getResponseCode();
System.out.println("Response Code " + rc);
if (rc != HttpConnection.HTTP_OK) {
showAlert("Server error returned");
} else {
playerForm.append("openInputStream");
is = c.openInputStream();
player = Manager.createPlayer(is, "audio/aac");
player.realize();
// get volume control for player and set volume to max
VolumeControl vc = (VolumeControl) player.getControl("VolumeControl");
if(vc != null)
{
vc.setLevel(100);
}
player.prefetch();
player.start();
}
} catch (Exception f) {
playerForm.append("Exception in player " + f.getMessage() + "\n");
} finally { // Aufräumarbeiten ausführen
if (is != null)
try {
is.close();
} catch (IOException e) {
}
if (c != null)
try {
c.close();
} catch (IOException e) {
}
}`
Thanks in advance.
Regards
Anish.

Instead of Exception in catch just use Throwable class it will handle OutOfMemoryError

Related

Shoutcast radio with J2ME

I have my application playing the radio stream which is shoutcast streaming. I have searched on the internet for 2 days but my application stills can't play this url. I don't know how to solve this.
This is my sample source code.
String url1 = "http://203.150.224.142:8003/;
HttpConnection con= (HttpConnection)Connector.open(url1);
InputStream is = con.openInputStream();
player = Manager.createPlayer(is, "audio/mpeg");
player.realize();
player.prefetch();
player.start();
I have used this code in production, it should work for you also.
HttpConnection conn = (HttpConnection) Connector.open(music.getTrack_url() + "?streamable=true", Connector.READ_WRITE);
if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
is = conn.openInputStream();
player = Manager.createPlayer(is, "audio/mp3");
player.addPlayerListener(thisObj);
player.realize();
player.prefetch();
player.start();
}
Try This.
public void loadShoutcast(String url)
{
StreamConnection connection = null;
int BUFFER_SIZE = 1024;
DataOutputStream dos_ = null;
OutputStream os_ = null;
try
{
System.out.println("opening connection " + url);
//Shoutcast URL
connection = (StreamConnection) Connector.open(url);
os_ = connection.openOutputStream();
dos_ = new DataOutputStream(os_);
// send the HTTP request
String req = "GET / HTTP/1.1\r\nUser-Agent: Profile/MIDP-1.0 Configuration/CLDC-1.0\r\n\r\n";
dos_.write(req.getBytes());
is = null;
is = connection.openInputStream();
long byteDone = 0;
int byteCount;
byte[] buffer = new byte[BUFFER_SIZE];
//Connection to file where you want to save the content of shoutcast radio
//It can be skipped in case you dont want to save the contents
out = tempFilecon.openDataOutputStream();
System.out.println("starting download");
while (byteCount = is.read(buffer)) >= 0)
{
out.write(buffer, 0, byteCount);
byteDone += byteCount;
done += byteCount;
}
return;
}
catch (InterruptedIOException e)
{
System.out.println("InterruptedIOException 1" + e.getMessage());
return;
}
catch (Exception e)
{
System.out.println("ERROR - 51 " + e.getMessage());
return;
}
finally
{
if (dos_ != null)
{
dos_.close();
dos_ = null;
}
if (os_ != null)
{
os_.close();
os_ = null;
}
if (is != null)
{
is.close();
is = null;
}
if (out != null)
{
out.close();
out = null;
}
System.out.println("closing connection");
if (connection != null)
{
connection.close();
connection = null;
}
}
// return false;
}

Open Web/URL through app

i'm developing J2ME.
Could you mind helping me to find out whats wrong with this code:
HttpConnection c = null;
try {
c = (HttpConnection)Connector.open("http://www.mysite.com",Connector.READ_WRITE, true);
c.setRequestMethod(HttpConnection.GET); //default
is = c.openInputStream(); // transition to connected!
int ch = 0;
for(int ccnt=0; ccnt < 150; ccnt++) { // get the title.
ch = is.read();
if (ch == -1){
break;
}
sb.append((char)ch);
}
}
catch (IOException x){
x.printStackTrace();
}
finally{
try {
is.close();
c.close();
} catch (IOException x){
x.printStackTrace();
}
}
System.out.println(sb.toString());
i'm trying open website through my app, thanks before :)
To open a URL in J2ME you do the following in a non-system thread:
MIDlet.platformRequest("http://www.mysite.com/");

How to post data to server in J2ME? Getting response from server Invalid API Key

I am posting some data to server but i am getting {"status":false,"error":"Invalid API Key."} response from server. My API Key is correct but where is the problem i don't know. Please friends help me. I tried lot but i did not find solution. I have used following code for posting data to server.
public void tryingOne(String dtl){
HttpConnection httpConn = null;
InputStream is = null;
OutputStream os = null;
String url="http://api.geanly.in/ma/index?API-Key="+apikey;
String details = "&orderInfo={\"booking\":{\"restaurantinfo\":{\"id\":\"5722\"},\"referrer\":{\"id\": \"9448476530\" }, \"bookingdetails\":{\"instructions\":\"Make the stuff spicy\",\"bookingtime\": \"2011-11-09 12:12 pm\", \"num_guests\": \"5\"}, \"customerinfo\":{\"name\":\"Vinodh SS\", \"mobile\":\"9345245530\", \"email\": \"vind#gmail.com\", \"landline\":{ \"number\":\"0908998393\",\"ext\":\"456\"}}}}";
try {
System.out.println("url####"+url);
httpConn = (HttpConnection) Connector.open(url);
httpConn.setRequestMethod(HttpConnection.POST);
DataOutputStream outStream = new DataOutputStream(httpConn.openDataOutputStream());
outStream.write(details.getBytes(),0,details.length());
outStream.flush();
outStream.close();
//Reading Resp. from server
StringBuffer sb = new StringBuffer();
is = httpConn.openDataInputStream();
int chr;
while ((chr = is.read()) != -1) {
sb.append((char) chr);
}
System.out.println("Response from srvr " + sb.toString());
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
if (httpConn != null) {
try {
httpConn.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Please help me friends...
Thanks
you need to trim the variable "apikey".
I was getting same problem when connecting with Picasa Server.
For sending data using HttpConnection use the class in this tutorial. It also gets the response from the server.

how to store the data in a file in j2me..?

please can anyone tell me how to store the RMS data to file and how to modify it?
You can use this code for write the data in a file.
private void writeTextFile(String fName, String text) {
DataOutputStream dos = null;
FileConnection fconn = null;
try {
fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
if (!fconn.exists())
fconn.create();
dos = fconn.openDataOutputStream();
dos.write(text.getBytes());
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (dos != null)
dos.close();
if (fconn != null)
fconn.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

Audio files not Playing in Blackberry

I'm trying to play a recorded wave file. While playing, an exception is thrown at the following statement:
Player player = Manager.createPlayer(is, "audio/mpeg");
My entire code for playing the wave file is as follows:
if (types[cnt].equals("audio/x-wav")) {
Class clazz = Class.forName("RecordAudio");
InputStream is =
clazz.getResourceAsStream("file:///SDCard/BlackBerry/original.wav");
//create an instance of the player from the InputStream
Player player = Manager.createPlayer(is, "audio/mpeg");
player.realize();
player.prefetch();
//start the player
player.start();
}
What could be the problem?
The function getResourceAsStream is for pulling resources from a JAR/COD file, not from the filesystem. Plus, this is simpler than you are making it. Just pass the file name and path to createPlayer, like so:
try {
String filename = "file:///SDCard/BlackBerry/original.wav";
Player player = javax.microedition.media.Manager.Manager.createPlayer( filename );
} catch (IOException e) {
System.out.println("Error creating player");
} catch (MediaException e) {
System.out.println("Error media type");
}
I believe it is because of wrong MIME type. Try this:
String fileName = "file:///SDCard/BlackBerry/original.wav";
String mimeType = "audio/x-wav";
String types[] = javax.microedition.media.Manager
.getSupportedContentTypes(null);
for (int cnt = types.length - 1; cnt >= 0; --cnt) {
if (types[cnt].equals(mimeType)) {
InputStream is = null;
FileConnection fconn = null;
try {
fconn = (FileConnection) Connector.open(
fileName, Connector.READ);
} catch (IOException e) {
System.out.println("Error reading file");
}
try {
is = fconn.openInputStream();
} catch (IOException e) {
System.out.println("Error opening stream");
}
Player player = null;
try {
player =
javax.microedition.media.Manager.createPlayer(
is, mimeType);
} catch (IOException e) {
System.out.println("Error creating player");
} catch (MediaException e) {
System.out.println("Error media type");
}
try {
player.realize();
} catch (MediaException e) {
System.out.println("Player cannot be released");
}
try {
player.prefetch();
} catch (MediaException e) {
System.out.println("Player cannot be prefetched");
}
// start the player
try {
player.start();
} catch (MediaException e) {
System.out.println("Player cannot be started");
}
}
}
Also see in console what kind of exception was thrown.

Resources