J'ai la classe "TTSManager" qui gére le "text to speech" :
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 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"); } }
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
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(); } }
Mais le text to speech ne fonctionne pas :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 Toast toast = Toast.makeText(context, "sender Number: "+ senderNum + ", message: " + message, duration);
Je vois pas de tout où est l'erreur. Merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 //Ça ne marche pas !!! ttsManager.initQueue(message);
Partager