android菜鸟开发遇到的小问题

来源:互联网 发布:希捷硬盘数据恢复软件 编辑:程序博客网 时间:2024/04/20 12:48

今天在设置一个单词阅读器,可是在手机上不能发出声音,而在模拟器上可以发出声音,求解。

代码:

package com.example.tts;

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Speech extends Activity {

 TextToSpeech tts;
 EditText editText;
 Button speech;
 Button record;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  tts = new TextToSpeech(this, new OnInitListener() {
   
   @Override
   public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS){
     
     int result = tts.setLanguage(Locale.US);
     
     System.out.println("result=" + result);
     System.out.println("TextToSpeech.LANG_COUNTRY_AVAILABLE=" + TextToSpeech.LANG_COUNTRY_AVAILABLE);
     System.out.println("TextToSpeech.LANG_AVAILABLE=" + TextToSpeech.LANG_AVAILABLE);
     
     
     
     if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
       && result != TextToSpeech.LANG_AVAILABLE){
      Toast.makeText(Speech.this, "TTS暂不支持这种语言的朗读", Toast.LENGTH_LONG).show();
     }
    }
    
   }
  });
  
  editText = (EditText)findViewById(R.id.txt);
  speech = (Button)findViewById(R.id.speech);
  record = (Button)findViewById(R.id.record);
  
  speech.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    tts.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD, null);
    
   }
  });
  record.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    tts.synthesizeToFile(editText.getText().toString(), null, "/mnt/sdcard/sound.wav");
    
    Toast.makeText(Speech.this, "声音记录成功!", Toast.LENGTH_LONG).show();
    
   }
  });
  
 }

 @Override
 public void onDestroy(){
  if(tts != null){
   tts.shutdown();
  }
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

 

 

在手机上会出现红色字体部分,而在模拟器上运行会,可以发出声音。

原创粉丝点击