11-01-2012 08:58 AM - edited 03-14-2019 10:46 AM
Hi,
Is there a way to convert a UCCX voice prompt to base64 string format so that I can send it to an application server using an HTTP post?
Thanks,
Kevin
Solved! Go to Solution.
11-07-2012 01:01 AM
Hi,
I tested it this morning and it should work - I replaced one line (try) in order to catch all errors, not just the ones generated by the Commons Codec library.
In my script, I've got two variables:
- voicePrompt - type Document
- voiceString - type String
And the script itself:
1. Start
2. Set voicePrompt = P[800383383.wav]
3. Set voiceString =
{
java.io.InputStream in = null;
int bufSize = 16384;
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
try {
in = voicePrompt.getInputStream();
byte[] bytesRead = new byte[bufSize];
int bytesReadLength = 0;
while ((bytesReadLength = in.read(bytesRead)) > 0 ) {
baos.write(bytesRead,0,bytesReadLength);
}
return org.apache.commons.codec.binary.Base64.encodeBase64String(baos.toByteArray());
} catch (Exception e) {
return null;
}
}
4. End
The first Set step at 2 is used to create a reference to a prompt. In my case, it's already in the repository, as a file, named 800383383.wav. Of course, I could have used a different way, too, for instance, a Recording step. The key is to have a Document type variable, containg the prompt you want to encode.
The second Set step at 3 actually does all the work. First, we create an empty ByteArrayOutputStream, and then - within a try..catch block, we get the InputStream object of the voicePrompt. Next, we read 16384 bytes chunks of it, until the stream ends, and then we call the static Base64.encodeBase64String, where the argument is actually a byte array, created by calling the toByteArray() method on the baos ByteArrayOutputStream.
So when I tried debugging, this is what popped up:
So the voiceString variable now contains the base64 encoded form of the 800383383.wav binary file.
G.
11-01-2012 10:14 AM
Hi,
yes, there is. Some Java coding required and an external library (Apache Commons Codec).
Basically, you can get an InputStream object from the Prompt, get the whole thing as a byte array, and then use the ASF
encodeBase64String function to convert the byte[] to a String which would be actually the Base64 encoded form of the binary Prompt.
G.
P.S. To be quite honest with you, I don't really like the idea of pushing prompts over HTTP - is there any other way?
11-01-2012 11:11 AM
Can you use ftp or sftp. I do it all the time from within my app.
Hemal
11-02-2012 05:10 AM
Thanks for the responses. it would be possible to use another fiel transfer method such as FTP, however there is additional META data that will also be sent in the HTTP Post, therefore the solution seems cleaner if all the data can be sent in one transaction using a one transfer method.
I've downloaded the Apache Commons Codec library which I'll try and use.
11-02-2012 07:13 AM
Hm, alright then. If you get stuck, let me know, I can help you with coding. Make sure you've got UCCX Premium or IP IVR.
G.
11-06-2012 09:05 AM
Hi Gergely,
I've got the Apache Commons Codec installed and have created the code to convert a Byte Array into a base64 string. However I stuck trying to get the Prompt into a Byte Array.
As I understand it, I can convert a prompt to a Hex string just by using a Set step:
1. Set voiceString = voicePrompt - is this correct?
2. Then I can use your code from another post to convert the String from Hex to a Byte array:
https://supportforums.cisco.com/docs/DOC-27417
3. Use the Commons Codec to convert the Byte Array to Base64 string.
Do these steps sound correct?
Thanks,
Kevin
11-06-2012 09:41 AM
Hi,
well, actually, this is what you need to do:
0. /* use the necessary steps to have a prompt, let's say, voicePrompt */
1. Set voiceString = {
java.io.InputStream in = null;
int bufSize = 16384;
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
in = voicePrompt.getInputStream();
byte[] bytesRead = new byte[bufSize];
int bytesReadLength = 0;
while ((bytesReadLength = in.read(bytesRead)) > 0 ) {
baos.write(bytesRead,0,bytesReadLength);
}
try {
return org.apache.commons.codec.binary.Base64.encodeBase64String(baos.toByteArray());
catch (Exception e) {
return null;
}
}
If everything goes correctly, the voiceString String variable will contain your base64 encoded prompt, from the voicePrompt variable (which is supposed to be of type Document).
Unfortuantely, I cannot test this at this moment, I will give it a try tomorrow.
G.
11-07-2012 01:01 AM
Hi,
I tested it this morning and it should work - I replaced one line (try) in order to catch all errors, not just the ones generated by the Commons Codec library.
In my script, I've got two variables:
- voicePrompt - type Document
- voiceString - type String
And the script itself:
1. Start
2. Set voicePrompt = P[800383383.wav]
3. Set voiceString =
{
java.io.InputStream in = null;
int bufSize = 16384;
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
try {
in = voicePrompt.getInputStream();
byte[] bytesRead = new byte[bufSize];
int bytesReadLength = 0;
while ((bytesReadLength = in.read(bytesRead)) > 0 ) {
baos.write(bytesRead,0,bytesReadLength);
}
return org.apache.commons.codec.binary.Base64.encodeBase64String(baos.toByteArray());
} catch (Exception e) {
return null;
}
}
4. End
The first Set step at 2 is used to create a reference to a prompt. In my case, it's already in the repository, as a file, named 800383383.wav. Of course, I could have used a different way, too, for instance, a Recording step. The key is to have a Document type variable, containg the prompt you want to encode.
The second Set step at 3 actually does all the work. First, we create an empty ByteArrayOutputStream, and then - within a try..catch block, we get the InputStream object of the voicePrompt. Next, we read 16384 bytes chunks of it, until the stream ends, and then we call the static Base64.encodeBase64String, where the argument is actually a byte array, created by calling the toByteArray() method on the baos ByteArrayOutputStream.
So when I tried debugging, this is what popped up:
So the voiceString variable now contains the base64 encoded form of the 800383383.wav binary file.
G.
11-07-2012 02:12 AM
Thanks Gergely that is working perfectly!
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide