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
Posts: 43
Registered: ‎11-24-2011
My Device: Perl
My Carrier: BB

Play audio from byte array

Hi,

I developed a web service where I got the byte array of recorded sound from device then I parse the xml & I got the byte array again SO I want to play this byte array  as mp3 or any other fromat which is supported by Blackberry

 

Folowing is my code plz help me and post your code

private void Play_audio()
        {
            
            try
            {
                

                
              ;
                
                
                SOP("***454***********4545 "+"mp3 lenght "+audio_mp3_new.length);
                
                

                bi = new ByteArrayInputStream(audio_mp3_new,0,audio_mp3_new.length-1);
                
                SOP("**********---------------******************** 0");
                
                player = javax.microedition.media.Manager.createPlayer(bi, "audio/mpeg");
                SOP("**********---------------******************** 1");
                player.realize();
                SOP("**********---------------******************** 2");
                VolumeControl volume = (VolumeControl)player.getControl("VolumeControl");
                SOP("**********---------------******************** 3");
                volume.setLevel(100);
                SOP("**********---------------******************** 4");
                player.setLoopCount(1);
                SOP("**********---------------******************** 5");
                player.start();
                SOP("**********---------------******************** 6");             
                
                
                SOP("sjghssjhh ********* sgsjkghs "+myAudioRecorderThread.getPriority());
            }
            catch(Exception e)
            {
                SOP("error at Play mp3 "+e);
            }

}

 

Following is the out put on my console when I check for the device debbuing :-

***454***********4545 [B@825f89d9mp3 lenght 38240
**********---------------******************** 0
**********---------------******************** 1
**********---------------******************** 2
**********---------------******************** 3
**********---------------******************** 4
**********---------------******************** 5
[STREAM] allocating buffer [index: 0] [size: 65536]
[STREAM] allocating stream [index: 0] [size: 65536]
[STREAM] allocated stream [index: 0] [handle: 11017] [size: 65536]
null:_ss=11017 PID=309
MN: init0(469762048, 0, 1, 65535)=0
MN: load 28
MP: h=469762048,s=LG: +mErr
MP: h=469762048,s=LG: +unlC
MN: finalize0(469762048)
MP: h=469762048,s=LG: -> UD
MP: h=469762048,s=UD: -unlC
MP: h=469762048,s=UD: -mErr
MP: h=469762048,s=UD: +sNMD
MP: h=469762048,s=UD: -sNMD
MP: h=469762048,s=UD: +unld(309):309
MP: h=469762048,s=UD: -unld
MP: h=469762048,s=UD: LOAD ERR
error at Play mp3 net.rim.device.internal.media.RimMediaException: Media unloaded while initializing
MP: h=469762048,s=UD: +unld(309):63
MP: h=469762048,s=UD: -unld
[SREAM] releasing buffer [index: 0] [handle: 11017]

 

Plz give me the simple code help me

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

Re: Play audio from byte array

In similar code that I have, there is a prefetch between the realize and the start.  Not sure of its significance.  Otherwise the code is similar.

 

Can I suggest you test your code by adding a file containing sound to your application, and instead of downloading the bytes, read the file into a byte array.  Then let the processing continue as though the bytes had come down.  If this works, then there is likely to be a problem in the data you have obtained from the XML - perhaps there is an encoding issue.  Anyway, add a built-in audio file and use it to prove your code. 

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

Re: Play audio from byte array

Hi,

Thnks for your solution .

 

I play the mp3 file from server as well as from device also. but My problem is that on this application I save byte array on mysql db which base64 encode value which I read from server on xml this xml i parse on blackberry

 

I can't create mp3 file on device as well as on server to play it from server

 

Please help me on this code

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

Re: Play audio from byte array

"I can't create mp3 file on device as well as on server to play it from server"

Sorry this is not clear to me.

 

But before we investigate this, can you please confirm that your code on the BlackBerry works fine when processing a MP3 file (for example one that is on the device).

 

Assuming this is the case, the the problem has to be the processing of the data. 

 

From what you are saying, you store the MP3 file in a mySQL database on the Server.  And you store it as a Base64 encoded text String.  So to return this to the proper byte form you just need to Base64 decode it.  You could write a bit of code to do this or use Base64InputStream to convert it. 

 

I would make sure that your processing correctly 'round trips' a sample MP3 file.  So take a sample MP3, store it in your database, extract it and the Base64 decode it.  You should get back the bytes that you started with.  If you don't, go through the stages of your processing and figure out which one is causing the failure. 

 

 

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

Re: Play audio from byte array

[ Edited ]

I solve this thanks as per your last post u told mr about encription.

 

what ever byte array I read from xml is a base 64 encoded I create a code and play the sound

 

Code:-

 

On constructor:-

myAudioRecorderThread = new AudioRecorderThread();

private void Play_audio()
        {
            
            try
            {
                
                  byte[] decoded = Base64InputStream.decode(strAudio);    
                myAudioRecorderThread.run();    
                myAudioRecorderThread._audioArray=decoded;
                myAudioRecorderThread.play();
            }
            catch(Exception e)
            {
                SOP("error at Play mp3 "+e.getMessage());
            }
      
        }

 

Finally ir work

 

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

Re: Play audio from byte array

Great.  Can you mark this as solved then?  Instructions in Help link near top of screen if you need them.

 

By the way, I would not call Base64 encoding 'encryption'.  It is just a translation of binary data into text data, that is all. 

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

Re: Play audio from byte array

Sorry,

I can't find the solved option

 

Can u tell me how to make it solved question.

 

And Thanks for your advice .

 

On my application there is always a Application permission window occur when I take a photos from camera and for listing voice and getting lat lon for the user

 

So I want to hide the Application permission window programatically Can u help to solve this problem plz

 

Thanks in Advance

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

Re: Play audio from byte array

This Thread is talking about handling audio files.  You generally only have one problem on a Thread.  So is the problem of the audio bytes been resolved?  If it has, then this Thread should be marked as solved.  If not, then can you tell me what the situation is now.

 

Regarding you other issue - the permissions popup - you should start a new Thread for that.  Before you do however, search the forum for similar questions, I think you will find that there are some.  For most applications, such as those downloaded from App World, there is no way around this.