2

I am using google speech api with cordova application.

I am recording the audio and saving it to my local drive using cordova plugin "cordova-plugin-media" after that i am reading back the file using cordova-plugin-file's readAsDataUrl() method. Which is returning me base64 data.

If i pass this base 64 data into the google api server it will always return me empty result without any errors. Below is the code for the same.

Please help me if anybody has solved similar issue.

var src = "cdvfile://localhost/temporary/recording.m4a";
                    var mediaRec = new Media(src,
                        // success callback
                        function (data) {
                            console.log(data);

                        },

                        // error callback
                        function (err) {

                        });

                    // Record audio
                    //mediaRec.startRecord();

                    var _options = {
                        SampleRate: 16000,
                        NumberOfChannels: 1
                    };

                    mediaRec.startRecordWithCompression(_options);

$cordovaFile.readAsDataURL('cdvfile://localhost/temporary/', 'recording.m4a')
                        .then(function (success) {
                            console.log(success);

                            mediaRec.play();

                            var _payload = {
                                config: {
                                    encoding: 'LINEAR16',
                                    sampleRate: 16000,
                                    languageCode: 'en-US',
                                    maxAlternatives: 30
                                },
                                audio: {
                                    content: success.split(',')[1]
                                }
                            };                          

                            $http.post('https://speech.googleapis.com/v1beta1/speech:syncrecognize?key={{api key}}', _payload)
                                .then(function (data) {
                                    console.log(data);
                                }, function (error) {
                                    console.log(error);
                                });

                        }, function (error) {
                            console.log(error);
                        }); 
Clemens Tolboom
  • 1,431
  • 16
  • 25
  • had you monitored the request ? is your request is working correctly ? – Hassan ALi Aug 26 '16 at 12:50
  • Yes @HassanALi . Even i am getting success response with 200 status code. but returned object will be empty – Santosh Kurdekar Aug 29 '16 at 09:31
  • Please format your code properly. You are sending an .m4a file. Is that supported? https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionConfig#AudioEncoding ... you could try the __"uri":"gs://cloud-samples-tests/speech/brooklyn.flac"__ first – Clemens Tolboom Sep 01 '16 at 11:32
  • Possible duplicate of [Google Speech Recognition API Result is Empty](http://stackoverflow.com/questions/38906527/google-speech-recognition-api-result-is-empty) – Nikolay Shmyrev Feb 16 '17 at 19:49

0 Answers0