在应用程序中嵌入语音,Android

时间:2011-10-25 09:42:16

标签: android

这是我的代码的正文,我想用TextToSpeech将声音嵌入“Color is RED”,我不知道在哪里以及如何把它 通过为String和TextToSpeech定义变量,它总是有问题。 有什么帮助吗?

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     Button buttonLoadImage = (Button)findViewById(R.id.loadimage);

     targetImage = (ImageView)findViewById(R.id.targetimage);

     buttonLoadImage.setOnClickListener(new Button.OnClickListener(){

 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  Intent intent = new Intent(Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  startActivityForResult(intent, 0);
 }});
 }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK){
 Uri targetUri = data.getData();
 Bitmap bitmap;
 try {
  bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
  targetImage.setImageBitmap(bitmap);
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }

}

final TextView textView = (TextView)findViewById(R.id.textView); 
final TextView textViewCol = (TextView)findViewById(R.id.textViewColor);
final TextView textViewVal = (TextView)findViewById(R.id.textViewValue);

targetImage.setOnTouchListener(new ImageView.OnTouchListener(){     


    TextToSpeech mTts ;

    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub       
        textView.setText("Touch coordinates : " +       
        String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
        ImageView imageView = ((ImageView)v);
        Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
        int pixel = bitmap.getPixel((int)event.getX(),(int)event.getY());

        textViewVal.setText("[" + Color.red(pixel) + "," + Color.green(pixel) + ","     + Color.blue(pixel) + "]");

        if ((Color.red(pixel) > 200) && (Color.blue(pixel) < 10) &&     (Color.green(pixel) < 10)) {
               textViewCol.setText("Color is Red.");       
            }
return true;    }     
});

}

protected int valueOf(float x) {
    // TODO Auto-generated method stub
    return 0;
}
}

1 个答案:

答案 0 :(得分:1)

您应该阅读此处的文章:http://developer.android.com/resources/articles/tts.html

但要做一些简单的文本到语音,请使用类似的代码:

    private TextToSpeech mTts;
private ImageView targetImage;

private TextView textView;
private TextView textViewCol;
private TextView textViewVal;
private boolean hasTTSBeenInitialized = false;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.loadimage);

textView = (TextView) findViewById(R.id.textView);
textViewCol = (TextView) findViewById(R.id.textViewColor);
targetImage = (ImageView) findViewById(R.id.targetimage);
textViewVal = (TextView) findViewById(R.id.textViewValue);

buttonLoadImage.setOnClickListener(new Button.OnClickListener() {

    @Override
    public void onClick(View arg0) {
    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 0);
    }
});

//Initialise tts here so you can use it at anytime in the activity.
mTts = new TextToSpeech(this, new OnInitListener() {      
    @Override
    public void onInit(int status) {
    hasTTSBeenInitialized = true;
    }
  });

  mTts.setLanguage(Locale.US);   
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {
    Uri targetUri = data.getData();
    Bitmap bitmap;
    try {
    bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
    targetImage.setImageBitmap(bitmap);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

}

targetImage.setOnTouchListener(touchListener);

}

OnTouchListener touchListener = new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {

    textView.setText("Touch coordinates : " + String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
    ImageView imageView = ((ImageView) v);
    Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    int pixel = bitmap.getPixel((int) event.getX(), (int) event.getY());

    textViewVal.setText("[" + Color.red(pixel) + "," + Color.green(pixel) + "," + Color.blue(pixel) + "]");

    if ((Color.red(pixel) > 200) && (Color.blue(pixel) < 10) && (Color.green(pixel) < 10)) {
    //only attempt to play if tts has been initialised, if it hasnt then play it later when it has been initialised
    if(hasTTSBeenInitialized){
        mTts.speak("Color is Red", TextToSpeech.QUEUE_FLUSH, null);
    }else{
        //Maybe add it to a queue to play when it has been initialized
    }
    textViewCol.setText("Color is Red.");
    }
    return true;
}
};