Audio files not Playing in Blackberry - audio

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.

Related

Audio Streaming OutOfMemoryError

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

Download Images in loop java me

In my application we have to download around 10 Images from server and display it in mobile. How can I do this? Can I use same HttpConnection for full download? Is any other way for download?
You can do with this simple loop (Supposing imageList is a List with the url of the images).
HttpConnection = null;
Image image = null;
for (int i = 0; i < imageList.getSize(); i++) {
try{
String urlImage = imageList.get(i);
hc = (HttpConnection) Connector.open(urlImage);
image = Image.createImage(hc.openInputStream()));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
hc.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
You can try following method in a loop for downloading images from server.
private void downloadImage ( String URL )
{
try
{
System.out.println("URL FOR POST_DATA : "+URL);
// Open up a http connection with the Web server for both send and receive operations
httpConnection = (HttpConnection)Connector.open(URL, Connector.READ_WRITE);
// Set the request method to POST
httpConnection.setRequestMethod(HttpConnection.POST);
// Set the request headers
httpConnection.setRequestProperty(ConstantCodes.ACTION_MODE_PARAMETER,action);
httpConnection.setRequestProperty(ConstantCodes.USER_NAME_REQUEST_PARAMETER,userName);
if(eventName==null || eventName.equals(""))
eventName="default";
httpConnection.setRequestProperty(ConstantCodes.EVENT_NAME_REQUEST_PARAMETER, eventName);
httpConnection.setRequestProperty(ConstantCodes.CAMERAID_REQUEST_PARAMETER, cameraID);
// all the headers are sending now and connection chanel is establising
dis = httpConnection.openDataInputStream();
int ch = 0;
ByteArrayOutputStream bytearray = new ByteArrayOutputStream(250000);
while((ch = dis.read()) != -1)
bytearray.write(ch);
// fileByte contains whole file in bytes
byte fileByte[] = bytearray.toByteArray();
fileSize = fileByte.length;
System.out.println("Got file size : "+fileSize);
if(bytearray!=null) bytearray.close();
midlet.getLastPostedImageResponse(fileByte);
}
catch (IOException ioe)
{
ioe.printStackTrace();
System.out.println("IOException occured during getting last image data : "+ioe.getMessage());
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Eeception occurred during getting last image data : "+e.getMessage());
}
finally
{
System.out.println("Calling close from Last image posted Action");
close();
}

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.

Reading a file in Blackberry

I am trying to read content from file in blackberry and i read the file content to result object and when i am trying to print the result object it gives error and the file is in res folder,and this is code that i got problem
private String readTextFile(String fName) {
String result = null;
FileConnection fconn = null;
DataInputStream is = null;
try {
fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
is = fconn.openDataInputStream();
byte[] data = IOUtilities.streamToBytes(is);
result = new String(data);
System.out.println("result data is ...... "+result);
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (null != is)
is.close();
if (null != fconn)
fconn.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
return result;
}
what is fName here ?
The file name or the path could cause similar problem.

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());
}
}
}

Resources