- Notifications
You must be signed in to change notification settings - Fork94
🔆 Android SDK to use the IBM Watson services.
License
watson-developer-cloud/android-sdk
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Android client library to assist with using theWatson services, a collection of RESTAPIs and SDKs that use cognitive computing to solve complex problems.
'com.ibm.watson.developer_cloud:android-sdk:0.6.0'
Download the aarhere.
The minimum supported Android API level is 19. Now, you are ready to see someexamples.
The examples below assume that you already have service credentials. If not, you will have to create a service inIBM Cloud.
- Sign up for anIBM Cloud account.
- Create an instance of the Watson service you want to use and get your credentials:
- Go to theIBM Cloud catalog page and select the service you want.
- Log in to your IBM Cloud account.
- ClickCreate.
- ClickShow to view the service credentials.
- Copy the
apikey
value, or copy theusername
andpassword
values if your service instance doesn't provide anapikey
. - Copy the
url
value.
Once you've followed the instructions above to get credentials, they should be added to theexample/res/values/credentials.xml
file shown below.
<resources><!-- Paste Language Translator information here--> <stringname="visual_recognition_iam_apikey"></string> <stringname="visual_recognition_url"></string><!-- Paste Speech to Text information here--> <stringname="speech_text_iam_apikey"></string> <stringname="speech_text_url"></string><!-- Paste Text to Speech information here--> <stringname="text_speech_iam_apikey"></string> <stringname="text_speech_url"></string></resources>
If you are having difficulties using the APIs or have a question about the IBMWatson Services, please ask a question ondW AnswersorStack Overflow.
You can also check out thewiki for some additional information.
This SDK is built for use with theWatson Java SDK.
The examples below are specific for Android as they use the Microphone and Speaker; for actual services refer to theJava SDK. You can use the provided example app as a model for your own Android app using Watson services.
Provides simple microphone access within an activity.
MicrophoneHelpermicrophoneHelper =newMicrophoneHelper(this);
The MicrophoneHelper object allows you to create new MicrophoneInputStream objects and close them. The MicrophoneInputStream class is a convenience class for creating anInputStream
from device microphone. You can record raw PCM data or data encoded using the ogg codec.
// record PCM data without encodingMicrophoneInputStreammyInputStream =microphoneHelper.getInputStream(false);// record PCM data and encode it with the ogg codecMicrophoneInputStreammyOggStream =microphoneHelper.getInputStream(true);
An example using a Watson Developer Cloud service would look like
speechService.recognizeUsingWebSocket(newMicrophoneInputStream(),getRecognizeOptions(),newBaseRecognizeCallback() {@OverridepublicvoidonTranscription(SpeechResultsspeechResults){Stringtext =speechResults.getResults().get(0).getAlternatives().get(0).getTranscript();System.out.println(text); }@OverridepublicvoidonError(Exceptione) { }@OverridepublicvoidonDisconnected() { }});
Be sure to take a look at the example app to get a working example of putting these all together.
Provides the ability to directly play anInputStream
.Note: TheInputStream
must come from a PCM audio source. Examples include WAV files or Audio/L16.
StreamPlayerplayer =newStreamPlayer();player.playStream(yourInputStream);
Since this SDK is intended to be used with the Watson APIs, a typical use case for theStreamPlayer
class is for playing the output of a Watson Text to Speech call. In that case, you can specify the type of audio file you'd like to receive from the service to ensure it will be output properly by your Android device.
SynthesizeOptionssynthesizeOptions =newSynthesizeOptions.Builder() .text("I love making Android apps") .accept(SynthesizeOptions.Accept.AUDIO_WAV)// specifying that we want a WAV file .build();InputStreamstreamResult =textService.synthesize(synthesizeOptions).execute();StreamPlayerplayer =newStreamPlayer();player.playStream(streamResult);// should work like a charm
Another content type that works from the Text to Speech APIs is the Audio/L16 type. For this you need to specify the sample rate, and you can do so with the alternate version of theplayStream()
method. The default sample rate on the single-argument version is 22050.
SynthesizeOptionssynthesizeOptions =newSynthesizeOptions.Builder() .text("I love making Android apps") .accept("audio/l16;rate=8000")// specifying our content type and sample rate .build();InputStreamstreamResult =textService.synthesize(synthesizeOptions).execute();StreamPlayerplayer =newStreamPlayer();player.playStream(streamResult,8000);// passing in the sample rate
Provides simple camera access within an activity.
CameraHelpercameraHelper =newCameraHelper(this);cameraHelper.dispatchTakePictureIntent();@OverrideprotectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata) {super.onActivityResult(requestCode,resultCode,data);if (requestCode ==CameraHelper.REQUEST_IMAGE_CAPTURE) {System.out.println(cameraHelper.getFile(resultCode)); } }
Like the CameraHelper, but allows for selection of images already on the device.
To open the gallery:
GalleryHelpergalleryHelper =newGalleryHelper(this);galleryHelper.dispatchGalleryIntent();@OverrideprotectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata) {super.onActivityResult(requestCode,resultCode,data);if (requestCode ==GalleryHelper.PICK_IMAGE_REQUEST) {System.out.println(galleryHelper.getFile(resultCode,data)); } }
Testing in this SDK is accomplished withEspresso.
To run the tests, in Android Studio:
Within the example package, right-click the androidTest/java folder and click Run 'All Tests'.
UseGradle (version 1.x) to build and test the project you can use
Gradle:
$cd android-sdk$ gradletest# run tests
Find more open source projects on theIBM Github Page
This library is licensed under Apache 2.0. Full license text isavailable inLICENSE.
SeeCONTRIBUTING.md.
About
🔆 Android SDK to use the IBM Watson services.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors14
Uh oh!
There was an error while loading.Please reload this page.