2012-12-04 11 views
1

나는 사용자의 위치를 ​​찾기 위해 백그라운드 서비스를 작성했습니다. 이제 그의 위치가 변경되면 사용자에게 음성 알림을 보내려고합니다. 위치 변경을 결정하고 간단한 푸시 알림을 보내는 코드를 작성했습니다. 이제는 음성 알림 또는 음성 알림 코드 작성 방법에 대해 도움을 줄 수 있거나 음성 알림을 전송하는 제 3 자 라이브러리가 있습니다. 사용자. 감사합니다. 음성 API를 여기 see reference hereandroid는 사용자에게 음성 알림을 전송합니다.

답변

2

예 마지막으로 나는 매우 쉬운 방법으로 완벽한 솔루션을 얻었습니다.

private TextToSpeech myTTS; // Define the TTS objecy 
private int MY_DATA_CHECK_CODE = 0; 

//write the following code in oncreate method or whereever you want to use this 
{ 
    myTTS = new TextToSpeech(this, this); 

    Intent checkTTSIntent = new Intent(); 
    checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
    startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE); 
    speakWords("Pass the String here"); 
} 

private void speakWords(String speech) { 
    //speak straight away 
    //myTTS.setLanguage(Locale.US); 
    System.out.println(speech + " TTSTTTS"); 
    myTTS.speak(speech, TextToSpeech.LANG_COUNTRY_AVAILABLE, null); 
} 

public void onInit(int status) { 
    // TODO Auto-generated method stub 
    if (status == TextToSpeech.SUCCESS) { 
     if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE) 
      myTTS.setLanguage(Locale.US); 
    } else if (status == TextToSpeech.ERROR) { 
     Toast.makeText(getApplicationContext(), "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show(); 
    } 
} 
1

사용 안드로이드 텍스트는 그 부분을 여기에 붙여해야 할 수 있습니다 예를 들어 link이다. (notificationManager.notify(0, notification);가 호출) 알림 방법에

private TextToSpeech myTTS; 

Intent checkTTSIntent = new Intent(); 
     checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
     startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE); 
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null); 

speakWords(words); //call this function with the string you want as voice 

     //speak the user text 
private void speakWords(String speech) { 
     //speak straight away 
     myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null); 
} 
    //act on result of TTS data check 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == MY_DATA_CHECK_CODE) { 
     if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { 
      //the user has the necessary data - create the TTS 
     myTTS = new TextToSpeech(this, this); 
     } 
     else { 
       //no data - install it now 
      Intent installTTSIntent = new Intent(); 
      installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
      startActivity(installTTSIntent); 
     } 
    } 
} 
    //setup TTS and don't forget to implement OnInitListener interface to use this method 
public void onInit(int initStatus) { 
     //check for successful instantiation 
    if (initStatus == TextToSpeech.SUCCESS) { 
     if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE) 
      myTTS.setLanguage(Locale.US); 
    } 
    else if (initStatus == TextToSpeech.ERROR) { 
     Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show(); 
    } 
} 
1

, 다음을 추가하면 알림을 밀어 때마다

try { 
     Uri ring_uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), ring_uri); 
     r.play(); 
} catch (Exception e) { 
     // Error playing sound 
} 

이 알림 소리가납니다.

+0

안녕하세요, 저는이 소리를 사용자 정의 할 수 있습니까, 지금은 특정 문자열을 말하고 싶습니다. 최대한 빨리 회신 해주십시오 –

+0

재생하려는 각 음성의 음성 파일이있는 경우이 파일을 사용할 수는 있지만 문제가 아닌 것으로 생각합니다. TextToSpeech 클래스를 사용해야합니다. –

+0

Ok, 도움 주셔서 감사합니다. –

관련 문제