Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🔆 Android SDK to use the IBM Watson services.

License

NotificationsYou must be signed in to change notification settings

watson-developer-cloud/android-sdk

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.

Table of Contents

Installation

Gradle
'com.ibm.watson.developer_cloud:android-sdk:0.6.0'
AAR

Download the aarhere.


The minimum supported Android API level is 19. Now, you are ready to see someexamples.

Usage

The examples below assume that you already have service credentials. If not, you will have to create a service inIBM Cloud.

Service Credentials

Getting the Credentials

  1. Sign up for anIBM Cloud account.
  2. 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 theapikey value, or copy theusername andpassword values if your service instance doesn't provide anapikey.
    • Copy theurl value.

Adding the Credentials

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>

Questions

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.

Examples

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.

MicrophoneHelper

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.

StreamPlayer

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

CameraHelper

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

GalleryHelper

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

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'.

Build + Test

UseGradle (version 1.x) to build and test the project you can use

Gradle:

$cd android-sdk$ gradletest# run tests

Open Source @ IBM

Find more open source projects on theIBM Github Page

License

This library is licensed under Apache 2.0. Full license text isavailable inLICENSE.

Contributing

SeeCONTRIBUTING.md.

Packages

No packages published

Contributors14


[8]ページ先頭

©2009-2025 Movatter.jp