IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Problème d'utilisation de Text to speech


Sujet :

Android

  1. #1
    Membre du Club
    Inscrit en
    Mars 2006
    Messages
    94
    Détails du profil
    Informations forums :
    Inscription : Mars 2006
    Messages : 94
    Points : 40
    Points
    40
    Par défaut Problème d'utilisation de Text to speech
    J'ai la classe "TTSManager" qui gére le "text to speech" :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
     
    import android.app.Activity;
    import android.content.Context;
    import android.speech.tts.TextToSpeech;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.widget.Toast;
     
    import java.util.Locale;
     
    /**
     * Created by mayar on 15/01/16.
     */
    public class TTSManager {
        private TextToSpeech mTts = null;
        private boolean isLoaded = false;
     
        public void init(Context context) {
            try {
                mTts = new TextToSpeech(context.getApplicationContext(), onInitListener);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        private TextToSpeech.OnInitListener onInitListener = new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = mTts.setLanguage(Locale.US);
                    isLoaded = true;
     
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("error", "This Language is not supported");
                    }
                } else {
                    Log.e("error", "Initialization Failed!");
                }
            }
        };
        public void shutDown() {
            mTts.shutdown();
        }
     
        public void addQueue(String text) {
            if (isLoaded)
                mTts.speak(text, TextToSpeech.QUEUE_ADD, null);
            else
                Log.e("error", "TTS Not Initialized");
        }
     
        public void initQueue(String text) {
     
            if (isLoaded)
                mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
            else
                Log.e("error", "TTS Not Initialized");
        }
    }
    je souhaite l'uttiliser dans une autre classe qui gére la réception des SMS :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
     
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.speech.tts.TextToSpeech;
    import android.support.v4.content.LocalBroadcastManager;
    import android.support.v7.app.AppCompatActivity;
    import android.telephony.SmsManager;
    import android.telephony.SmsMessage;
    import android.util.Log;
    import android.widget.Toast;
     
    /**
     * Created by mayar on 14/01/16.
     */
    public class IncomingSms extends BroadcastReceiver   {
        private final String ACTION_RECEIVE_SMS = "android.provider.Telephony.SMS_RECEIVED";
        private TTSManager ttsManager = null;
     
        @Override
        public void onReceive(Context context, Intent intent) {
           if (intent.getAction().equals(ACTION_RECEIVE_SMS))
            {
                ttsManager = new TTSManager();
                ttsManager.init(context.getApplicationContext());
     
                Bundle bundle = intent.getExtras();
     
     
                try {
     
                    if (bundle != null) {
     
                        Object[] pdusObj = (Object[]) bundle.get("pdus");
     
                        int pdusLen = pdusObj.length;
                        for (int i = 0; i < pdusLen; i++) {
     
                            SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
     
                            String senderNum = currentMessage.getDisplayOriginatingAddress();;
                            String message = currentMessage.getDisplayMessageBody();
     
                            if (message.equalsIgnoreCase("yo"))
                            {
                                //Ça ne marche pas !!!
                                ttsManager.initQueue(message);
                                Log.i("YO App", "sender Number: " + senderNum + "; message: " + message);
     
                                // Show alert
                                int duration = Toast.LENGTH_LONG;
                                Toast toast = Toast.makeText(context, "sender Number: "+ senderNum + ", message: " + message, duration);
                                toast.show();
     
                               // yo1dot0MainActivity.getInstance().updateTheTextView(message);
     
                            }
     
     
     
                        } // end for loop
                    } // bundle is null
     
                } catch (Exception e) {
                    Log.e("SmsReceiver", "Exception smsReceiver" +e);
     
                }
            }
            else
                Toast.makeText(context, "uuuuuuuuuuu", Toast.LENGTH_LONG).show();
        }
    }
    quand j'envoie "yo" aux téléphone, j'ai bien le toast qui s'affiche:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
      Toast toast = Toast.makeText(context, "sender Number: "+ senderNum + ", message: " + message, duration);
    Mais le text to speech ne fonctionne pas :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     //Ça ne marche pas !!!
       ttsManager.initQueue(message);
    Je vois pas de tout où est l'erreur. Merci

  2. #2
    Membre du Club
    Inscrit en
    Mars 2006
    Messages
    94
    Détails du profil
    Informations forums :
    Inscription : Mars 2006
    Messages : 94
    Points : 40
    Points
    40
    Par défaut
    Résolu :
    dans la fonction onReceive :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Intent in  = new Intent(context, yoSHistoricActivity.class)
                                        .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                in.putExtra("text", String.valueOf("yo"));
                                context.startActivity(in);
    dans le code de l’activité :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
     
    public class <ton activité> extends AppCompatActivity implements TextToSpeech.OnInitListener {
    .....
     @Override
        protected void onCreate(Bundle savedInstanceState) {
          .....
          mTts = new TextToSpeech(this,
                    this  // TextToSpeech.OnInitListener
            );
     
            Bundle extras = getIntent().getExtras();
            if (extras != null)
                        text = extras.getString("text");
        }
     
    @Override
        public void onDestroy() {
            // Don't forget to shutdown!
            if (mTts != null) {
                mTts.stop();
                mTts.shutdown();
            }
     
            super.onDestroy();
        }
     
        // Implements TextToSpeech.OnInitListener.
        public void onInit(int status) {
            // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
            if (status == TextToSpeech.SUCCESS) {
                // Set preferred language to US english.
                // Note that a language may not be available, and the result will indicate this.
                int result = mTts.setLanguage(Locale.US);
                // Try this someday for some interesting results.
                // int result mTts.setLanguage(Locale.FRANCE);
                if (result == TextToSpeech.LANG_MISSING_DATA ||
                        result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    // Lanuage data is missing or the language is not supported.
                    Log.e(TAG, "Language is not available.");
                } else {
                    // Check the documentation for other possible result codes.
                    // For example, the language may be available for the locale,
                    // but not for the specified country and variant.
     
                    // The TTS engine has been successfully initialized.
                    // Allow the user to press the button for the app to speak again.
                    // Greet the user.
                    mTts.speak(text, TextToSpeech.QUEUE_ADD, null);
                }
            } else {
                // Initialization failed.
                Log.e(TAG, "Could not initialize TextToSpeech.");
            }
        }
    ....
    }

  3. #3
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Salut,

    J'ai essayé de faire une lib sur le sujet https://github.com/ffournier/TTS-Recognizer-Job.

    j'ai juste un souci sur les Headset BlueTooth qui déconne un peu si jamais tu as eu le problème .

    Merci.
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [2008] Problème sur utilisation bcp pour export dans fichier texte
    Par dimitrak dans le forum Outils
    Réponses: 2
    Dernier message: 06/07/2015, 08h09
  2. Réponses: 5
    Dernier message: 29/04/2014, 12h23
  3. Problème d'utilisation des fichiers textes à partir du JAR
    Par arbaoui_aek2005 dans le forum Général Java
    Réponses: 2
    Dernier message: 22/06/2011, 01h33
  4. Problème d'utilisation de Text::Reform
    Par cryptorchild dans le forum Modules
    Réponses: 5
    Dernier message: 19/04/2006, 21h02
  5. Problème d'utilisation de Mysql avec dev-c++
    Par Watchi dans le forum Dev-C++
    Réponses: 10
    Dernier message: 06/08/2004, 14h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo