我是Android的新手。我正在尝试创建一个呼叫者名称播音员应用程序。以下是我的代码。日志显示,代码正在执行正常,即文本图形构造函数正在执行并达到OnInit方法,直到最后,当我对其进行测试时,听不到音频。
。这是我的CustomBroadcastreceiver.java:
public class CustomBroadcastReceiver extends BroadcastReceiver {
protected String newSender;
@Override
public void onReceive(Context context, Intent intent) {
Log.v("", "logger WE ARE INSIDE!!!!!!!!!!!");
Intent i=new Intent(context,ReceiverActivity.class);
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr = bundle.getString("incoming_number");
Log.v("", "logger phoneNr: " + phoneNr);
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNr));
Cursor cursor = context.getContentResolver().query(uri,
new String[] { PhoneLookup.DISPLAY_NAME }, phoneNr, null, null);
if (cursor.moveToFirst()) {
newSender = cursor.getString(cursor
.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
i.putExtra("ID", newSender);
Log.d("","logger intent" + i.getStringExtra("ID"));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
cursor.close();
}
}
这是接收者。Java
public class ReceiverActivity extends Activity implements TextToSpeech.OnInitListener{
String name;
private TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiver);
Log.d("","logger reached ReceiverActivity");
tts = new TextToSpeech(this, this);
Log.d("","logger created tts object");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.receiver, menu);
return true;
}
@Override
public void onInit(int status) {
Log.d("","logger reached onInit");
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US); // java.Util.Locale for
// country codes
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
tts.speak(name, TextToSpeech.QUEUE_FLUSH, null);
Log.d("","logger speak executed");
}
}
你能告诉我我要在哪里出错吗?
首先,在活动类中初始化文本对语音对象,然后用字符串参数进行静态方法,其中包含文本到语音的语法。<<<<<<<<<<<<<<<</p>
并用您要宣布的字符串从广播员中调用此静态方法。
public static void speakOut(String message) {
System.out.println("Message is: " + message);
tts.speak(message, TextToSpeech.QUEUE_ADD, null);
}