Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
maheshqtech
Posts: 43
Registered: ‎11-24-2011
My Device: Perl

play audio from byte array

I recod sound aon "capture://audio?encoding=audio/amr" and stored the byte array of this on database then I read the hole byte array on xml

 

I stored it's value on string so my string containe the byte array value it will work. Then I convert the string value to byte array from string.getBytes() method

 

 I place the following code on button click but it not start the playing of the byte array. and I not get any sound

 

 

Dialog.alert("start Audio "+audio_value);
input = new ByteArrayInputStream(audio_value);
SOP("input -7 " +audio_value.length);
_player = Manager.createPlayer(input, "audio/amr");

_player.realize();

VolumeControl volume = (VolumeControl)_player.getControl("VolumeControl");
 volume.setLevel(100);
_player.prefetch();
 _player.start();

Can any one provide sample code for playing audio file from byte array.  and how to play from datasource plz 

 

Thanks in advance

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: play audio from byte array

Your approach of converting the byte array to a Stream and playing that is correct, I have different cod that does much the same sort of thing. 

 

I don't understand this:

"stored it's value on string so my string containe the byte array value it will work."

 

sounds should not be converted to Strings - Strings are sequences of characters, but sounds files are bytes that have no validity when interpreted as characters.  I suspect there is a problem in this part of the processing. Se if you can keep the sound file as bytes and never convert it to a String, and that might help.

 

The other thing I would try is to add a resource to your project and use that file as input rather than your one - that will test to see of your player is working correctly - if so, then the problem is in the file creatin/conversion. 

 

 

Contributor
maheshqtech
Posts: 43
Registered: ‎11-24-2011
My Device: Perl

Re: play audio from byte array

Hi thanks for solution.
I developed a web service.
In that first ! save the recording of a meeting to server in a DB.
Then I collect the completed meeting information via Id and the information contain the byte array value of recording.

Here I use the dom parsing so the node value can't be converted directly to byte array so first I convert it to string then the string contain the db's value of recorded voice .
Then for playing I again the convert the String to byte array using

ex:-
// xml code
Node value=firstAudioList.item(j).getChildNodes().item(0);
String strAudio=value.toString(); // string hold the DB value
byte[] audio_value= strAudio.getBytes(); //convert the string to byte array

//code to play the byte array

try {
Dialog.alert("start Audio "+audio_value);
input = new ByteArrayInputStream(audio_value);
_player = Manager.createPlayer(input, "audio/amr");
VolumeControl volume =(VolumeControl)_player.getControl("VolumeControl");
volume.setLevel(100);
_player.realize();
_player.prefetch();
_player.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

It will not work plz give me some solution

On the part of getting the data from xml which hold the DB value of audio byte array it give me proper way

Give me some kind of code plz help me
Thanks
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: play audio from byte array

I assume that you have in fact tried your audio playback code with a byte array that you know to be working, which means the problem is in the conversion of the bytes to String (characters) and back again.

 

This is actually a common problem.  There are a variety of ways of converting bytes if you intend to treat the bytes as characters and the common problem is that when converting to String, most people do new String(<bytes>) and don't specify UTF-8 as the encoding, but the other end assumes UTF-8.  So this is one thing that you could look at.  Most Servers use UTF-8, the BlackBerry by default using ISO-8891-1 (from memory) but you can tell the BB to use UTF-8. 

 

However the data you are storing is NOT character data, so while converting to character and converting back will mostly work, I'm not convinced it will always work.  Personally when I am sending binary data that I know needs to treated as characters (as you do because it comes back in an XML stream), then I Base64 encode it.  So you will send the Server the file Base64 encoded and then process the String you get back by Base 64 decoding it.

 

But the first step you need to do is to prove that this is the problem.  So take a recording and note the exact byte values that you send to the Server.  Then note the byte values you try to play when you get back from the Server what should be the same audio file.  If you compare the bytes and they are not the same, then you have your problem.

 

Remember that to output the bytes you will need to use something to hex printt the byte values, so look for a byte array to Hex method - there are a few samples of this on the forum if you can't find something elsewhere. 

Contributor
maheshqtech
Posts: 43
Registered: ‎11-24-2011
My Device: Perl

Re: play audio from byte array

can you tell me how to play a sound file from server 

 

if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)
{
p = javax.microedition.media.Manager.createPlayer("http://www.oddfractions.com/sales_app/images/"+strAudio_file+";interface=wifi");

p.getControl("audio/amr");

p.getContentType().concat("audio/amr");
}
else {


p = javax.microedition.media.Manager.createPlayer("http://www.oddfractions.com/sales_app/images/"+strAudio_file);
p.getControl("audio/amr");
p.getContentType().concat("audio/amr");
}


//Player p = Manager.createPlayer("http://www.oddfractions.com/sales_app/images/"+strAudio_file);

p.realize();

 VolumeControl volume = (VolumeControl)p.getControl("VolumeControl");

volume.setLevel(100);

p.prefetch();

p.start();

}
catch(Exception e)
{
SOP("Error @ AMR "+e);
}

 

this code give error

net.rim.device.internal.media.RimMediaException: Media unloaded while initializing

 

Can any one give the solution for this

 

Thanks In advance

 

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: play audio from byte array

Sorry, never tried streaming myself, I have always downloaded and then played the downloaded file. Search for other forum entries that talk about streaming