nativescript-community / texttospeech

Text to Speech NativeScript plugin for Android & iOS :loudspeaker:
http://nativescript-community.github.io/texttospeech/
MIT License
50 stars 24 forks source link

Speech working on simulator but not on Device #8

Closed ShyAlonSidekick closed 7 years ago

ShyAlonSidekick commented 7 years ago

Hi The plug-in works perfectly on the simulator but does not play anything on the device (which is not silenced or muted :-)). One of the differences I noticed between my application and the demo is that my application uses tns version 3 e.g. "tns-ios": { "version": "3.0.0" } Could that be why it's failing?

bradmartin commented 7 years ago

Shouldn't have any issues with NS 3+ since this wasn't affected by any breaking changes that I know of. I'll have to look into this later and see. I've actually never used it on an iOS device, don't own any iOS only Android here 👋 . I've only used it on iOS simulator myself but I've seen some apps with NS using it. @mikebranstein I think used it on a Pokedex app that ran on iOS and Android.

bradmartin commented 7 years ago

Ah - here is the code - https://github.com/mikebranstein/nativescript-pokedex/blob/master/app/pokedex-view-model.js#L5 you might try running that application and see if it works on your device.

ShyAlonSidekick commented 7 years ago

Awesome - thanks Thanks Shy Alon

ShyAlonSidekick commented 7 years ago

OK, so I pin pointed the issue but I don't know how to solve it:

import { Component, OnInit } from "@angular/core";
import { SpeechRecognition, SpeechRecognitionTranscription, SpeechRecognitionOptions } from 'nativescript-speech-recognition';
import { TNSTextToSpeech, SpeakOptions } from 'nativescript-texttospeech';
@Component({
    selector: "ns-speak",
    moduleId: module.id,
    templateUrl: "./speak.component.html",
})
export class SpeakComponent implements OnInit {
    options: SpeechRecognitionOptions;
    text: string;
    TTS: TNSTextToSpeech;
    constructor(private speech: SpeechRecognition) {
        this.options = {
            locale: 'EN_US',
            onResult: (transcription: SpeechRecognitionTranscription) => {
                console.log(`${transcription.text}`);
                console.log(`${transcription.finished}`);
                this.text = transcription.text;
            },
            returnPartialResults: false
        };
    }

    ngOnInit(): void {
        this.TTS = new TNSTextToSpeech();
    }

    triggerListening() {
        console.log('triggerListening');
        this.speech.available().then(result => {
            result ? this.startListening() : alert("Speech recognition not available");
        }, error => {
            console.error(error);
        })
    }

    startListening() {
        this.speech.startListening(this.options).then(() => {
            console.log('started listening');
        }, error => {
            console.error(error);
        })
    }

    sayIt() {
        const say = `I heard you say ${this.text}`;
        let speakOptions: SpeakOptions = {
            text: say, /// *** required ***
            speakRate: 0.45,
            finishedCallback: (() => {
                alert('Finished Speaking :)');
            }),
            //speakRate: 0.5, // optional - default is 1.0
            //pitch: 1.0, // optional - default is 1.0
            //volume: 1.0, // optional - default is 1.0
            language: "en-US"  // optional - default is system language,
            // finishedCallback: Function // optional
        }
        // Call the `speak` method passing the SpeakOptions object
        console.log('before speak', speakOptions);
        this.TTS.speak(speakOptions);
        this.TTS.resume();
        console.log('after speak');
    }

    stopListening() {
        this.speech.stopListening().then(() => {
            console.log('stopped listening');
        }, error => {
            console.error(error);
        });
    }
}

If I'm calling the sayIt method before listening it's all good and the speech is heard. However if I call startListening first and then sayIt now audio is heard and TNSTextToSpeech.prototype.speak fails silently (forgive the pun :-)). Any idea why?

p-3 commented 7 years ago

+1 .. i am having the same issue..

EddyVerbruggen commented 7 years ago

@ShyAlonSidekick This is not a problem with this plugin but rather the speech recognition plugin that you're also using. See https://github.com/EddyVerbruggen/nativescript-speech-recognition/issues/14

The fact that it works on the simulator is that the speech recognition plugin is not supported there, so it won't mess up the audiosettings required by this plugin.