2010-06-22 4 views
1

메시지를 수신 할 때 읽는 앱이 있습니다. pgm은 내가 시작할 때 또는 에뮬레이터에서 뒤로 버튼을 눌러도 제대로 작동하는 것처럼 보입니다. 그러나 동일한 에뮬레이터에서 다른 응용 프로그램을 시작한 다음 메시지가 수신되면 토스트로 메시지를 볼 수는 있지만 오전에 있습니다. 사운드를 수신 할 수 없으므로 어떻게 수정합니까?백그라운드에서 프로그램이 실행되고 있지 않습니다.

** TextSpeaker **

package com.example.TextSpeaker; 

import java.util.Locale; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.speech.tts.TextToSpeech; 
import android.speech.tts.TextToSpeech.OnInitListener; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 

// the following programme converts the text to speech 

public class TextSpeaker extends Activity implements OnInitListener { 
/** Called when the activity is first created. */ 
int MY_DATA_CHECK_CODE = 0; 
public static TextToSpeech mtts; 
public Button button,stop_button; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    button = (Button)findViewById(R.id.button); 
    stop_button=(Button)findViewById(R.id.stop_button); 

    Intent myintent = new Intent(); 
    myintent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
    startActivityForResult(myintent, MY_DATA_CHECK_CODE); 
    //edit text=(EditText)findViewById(R.id.edittext); 
} 

    public void buttonClickListener(View src){ 
    switch(src.getId()) 
    { 
    case(R.id.button): 
     Toast.makeText(getApplicationContext(), "The service has been started\n Every new message will now be read out", Toast.LENGTH_LONG).show(); 
      //Intent serviceintent = new Intent(this,SpeakerService.class); 
      //serviceintent.setAction(".SpeakerService"); 
      //startService(new Intent(this,SpeakerService.class)); 
     // startService(serviceintent); 
      break; 
    case(R.id.stop_button): 
     Toast.makeText(getApplicationContext(), "The service has been stopped\n ", Toast.LENGTH_LONG).show(); 
     mtts.stop(); 
     //stopService(new Intent(this,SpeakerService.class)); 
     break; 
    } 
    } 



    protected void onActivityResult(int requestcode,int resultcode,Intent data) 
    { 
    if(requestcode == MY_DATA_CHECK_CODE) 
    { 
     if(resultcode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) 
     { 
     // success so create the TTS engine 
     Log.v("TextSpeaker","Pico is installed in the system"); 
     mtts = new TextToSpeech(this,this); 
     mtts.setLanguage(Locale.ENGLISH); 

     } 
     else 
     { 
     //install the Engine 
     Intent install = new Intent(); 
     install.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
     startActivity(install); 
     } 
    } 

    } 
    public void onDestroy(Bundle savedInstanceStatBundle) 
    { 
    //mtts.shutdown(); 
    } 

    //public void onPause() 
    //{ 
    // super.onPause(); 
    // // if our app has no focus 
    // if(mtts!=null) 
    // mtts.stop(); 
    // } 
    @Override 
    public void onInit(int status) { 
    if(status==TextToSpeech.SUCCESS) 
    button.setEnabled(true); 


     }  
    } 

수신기

package com.example.TextSpeaker; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.speech.tts.TextToSpeech; 
import android.telephony.SmsMessage; // supports both gsm and cdma 
import android.util.Log; 
import android.widget.Toast; 



public class Receiver extends BroadcastReceiver{ 


public static String str=""; 
@Override 
public void onReceive(Context context, Intent intent) { 
Bundle bundle = intent.getExtras(); 
Log.d("Receiver","Message received successfully"); 

SmsMessage[] msgs = null; 

if(bundle!=null) 
{ 
// retrive the sms received 

Object[] pdus = (Object[])bundle.get("pdus"); 
msgs = new SmsMessage[pdus.length]; 
for(int i=0;i<msgs.length;i++) 
    { 
str=""; 
msgs[i]=SmsMessage.createFromPdu((byte[]) pdus[i]); 
str+="Message From "+msgs[i].getOriginatingAddress()+"."; 
str+="The message is "+msgs[i].getMessageBody().toString(); 
TextSpeaker.mtts.speak(Receiver.str, TextToSpeech.QUEUE_FLUSH,null); 
} 
Toast.makeText(context,str,Toast.LENGTH_LONG).show(); 



    } 
} 
} 
+0

뭔가 제안 했습니까? – pranay

+0

나는 logcat을 핥아서 새로운 앱을 시작할 때 토스트 메시지를 얻지 만 연설을 위해 "null synthesis-can't speak"을 얻는다. 누군가 왜 설명 할 수 있겠는가? – pranay

답변

0

음성 합성 라이브러리는 안드로이드 장치에 기본적으로 설치되지 않았기 때문에이 가능성이 높습니다. 안드로이드 마켓을 방문하여 Speech Synthesis Language Pack을 설치 한 다음 다시 시도하십시오.

기본적으로 모든 장치에 새로운 장치가 설치되어있는 것은 아닙니다.

관련 문제