- Create an android application with Xamarin.Forms to take record audio and display the display what you said using the Bing Speech API.
- Download and install Visual Studio for your platform.
- Download or clone the repository and navigate to the correct start files and open SpeechToText.sln in Visual Studio. The starter solution is a Xamarin.Forms solution which has the following Nuget packages installed and ready for use:
- Plugin.AudioRecorder: for easy cross.platform audio recording
- Newtonsoft.Json: for serializing and deserializing JSON.
- To debug the app a physical android device is highly recommended, because the app will need to record audio.
- If everything went alright, you should be able to build and run the starter app in Visual Studio. (The app is just an empty white screen)
- To use the Bing Speech Api, retrieve a free subscription key here
- Create a UI
- Add two buttons: one to start/stop recording and one to send your audio to the API.
- Add a label to display the result text.
- Audio
- Initialize an AudioRecorderService.
- On the RecordButton click event, use the AudioRecorderSerivce instance to record audio.
- Api
- To send a request, you will need to request a Jason WebToken using your free subscription key.
- Create an instance of an HttpClient (save it in a class-wide variable for re-use) and add your subscription key to the DefaultRequestHeader as an "Ocp-Apim-Subscription-Key".
- Send a PostAsync request to "https://api.cognitive.microsoft.com/sts/v1.0/issueToken" without a body. The content of the result can be read as a string, which is your Bearer token. Now you can make the request to the Bing Api and send your audio file:
- First set your required bearer token to the HttpClient instance DefaultRequestHeaders.Authorization by setting it to a new AuthenticationHeaderValue.
- Create a new StreamContent instance with the Stream of the recorded audiofile.
- Set the Content-Type Headers of the streamcontent instance to the correct audio format: "audio/wav;codec="audio/pcm";samplerate=16000".
- Send a PostAsync request to the correct http endpoint and pass in the StreamContent instance. Await and read the content of the response as a string.
- Finally, update the label for the result text with the response and test your app!
- To send a request, you will need to request a Jason WebToken using your free subscription key.
- Bing Speech API documentation: Explanation about the Bing Speech API, requests, responses etc.
- Developer.Xamarin Bing Speech example: for some useful code snippets