Last active
July 3, 2016 15:48
-
-
Save quangson91/12415a77da8b02d172e88ca78c19c44b to your computer and use it in GitHub Desktop.
Demo bing speech api for ANDROID.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vnzit.testbingspeechapi; | |
import com.google.gson.Gson; | |
import org.apache.commons.io.IOUtils; | |
import java.io.InputStream; | |
import java.util.concurrent.TimeUnit; | |
import okhttp3.FormBody; | |
import okhttp3.HttpUrl; | |
import okhttp3.MediaType; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.RequestBody; | |
import okhttp3.Response; | |
import static org.junit.Assert.assertNotNull; | |
/** | |
* Created by sh on 7/3/16. | |
*/ | |
public class BingSpeechApi { | |
/** | |
* Get string json response from Bing Speech API. | |
* | |
* @param file WAV audio file. | |
*/ | |
public String recognize(final String file, final String bingApiKey) throws Exception { | |
final OkHttpClient client = new OkHttpClient.Builder() | |
.readTimeout(1, TimeUnit.MINUTES) | |
.writeTimeout(1, TimeUnit.MINUTES) | |
.build(); | |
final Gson gson = new Gson(); | |
final RequestBody bodyIssueToken = new FormBody.Builder() | |
.add("grant_type", "client_credentials") | |
.add("client_id", bingApiKey) | |
.add("client_secret", bingApiKey) | |
.add("scope", "https%3A%2F%2Fspeech.platform.bing.com") | |
.build(); | |
final Request requestIssueToken = new Request.Builder() | |
.url("https://oxford-speech.cloudapp.net/token/issueToken") | |
.post(bodyIssueToken) | |
.build(); | |
final Response responseToken = client.newCall(requestIssueToken).execute(); | |
final Token token = gson.fromJson(responseToken.body().charStream(), Token.class); | |
System.out.println(token.toString()); | |
assertNotNull(token.getAccessToken()); | |
final InputStream in = this.getClass().getClassLoader().getResourceAsStream(file); | |
final HttpUrl urlRecognize = HttpUrl.parse("https://speech.platform.bing.com/recognize") | |
.newBuilder() | |
.addQueryParameter("scenarios", "smd") | |
.addQueryParameter("appid", "D4D52672-91D7-4C74-8AD8-42B1D98141A5") | |
.addQueryParameter("device.os", "Android") | |
.addQueryParameter("version", "3.0") | |
.addQueryParameter("instanceid", "565D69FF-E928-4B7E-87DA-9A750B96D9E3") | |
.addQueryParameter("requestid", "2065F912-3699-408C-A80A-9D17F42B9692") | |
.addQueryParameter("format", "json") | |
.addQueryParameter("locale", "en-us") | |
.build(); | |
final RequestBody bodyRecognize = RequestBody.create( | |
MediaType.parse("audio/wav; codec=\"audio/pcm\"; samplerate=8000"), | |
IOUtils.toByteArray(in) | |
); | |
final Request requestRecognize = new Request.Builder() | |
.url(urlRecognize) | |
.header("Authorization", "Bearer " + token.getAccessToken()) | |
.post(bodyRecognize) | |
.build(); | |
final Response responseRecognize = client.newCall(requestRecognize).execute(); | |
return responseRecognize.body().string(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vnzit.testbingspeechapi; | |
import com.google.gson.Gson; | |
import org.apache.commons.io.IOUtils; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import java.io.InputStream; | |
import java.util.concurrent.TimeUnit; | |
import okhttp3.FormBody; | |
import okhttp3.HttpUrl; | |
import okhttp3.MediaType; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.RequestBody; | |
import okhttp3.Response; | |
import static org.junit.Assert.assertNotNull; | |
/** | |
* To work on unit tests, switch the Test Artifact in the Build Variants view. | |
*/ | |
public class BingSpeechApiUnitTest { | |
@Test | |
public void is_bing_speech_api_work() throws Exception { | |
final String response = new BingSpeechApi().recognize("about_vn.wav", "YOUR_KEY"); | |
System.out.println(response); | |
Assert.assertNotNull(response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment