Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android采用語音文件形式讓TextView和EditText發出聲音

android采用語音文件形式讓TextView和EditText發出聲音

編輯:關於Android編程

讓TextView和EditText發出聲音在撥打電話等應用中很常見,如果采用TTS語音引擎未免太麻煩!所以可以采用MediaPlayer播放語音文件的形式

下面是關鍵代碼:

關鍵變量定義

	private TextView numInput;
	private int length = 0; //當前字符串長度
	private int beforeLength = 0;//上一次字符串長度
	private MediaPlayer mediaPlayer;
	private ArrayList mListeners = null;
	EditTextWatchTEL watch;
初始化使用方法
                watch = new EditTextWatchTEL();
		numInput.addTextChangedListener(watch);
		if (mListeners == null) {
			mListeners = new ArrayList();
		}
		mListeners.add(watch);

主要代碼

private class EditTextWatchTEL implements TextWatcher {
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}
		@Override
		public void afterTextChanged(Editable s) {
			if (length > 0) {
				if (beforeLength <= length) {
					beforeLength = length;
					List list = new ArrayList();
					for(int i=0;i<10;i++){
						list.add(String.valueOf(i));
					}
					String num = numInput.getText().toString().substring(beforeLength - 1);
					if(list.contains(num))
					{
						int value = Integer.parseInt(num);
						switch (value) {
						case 0:
							if(mediaPlayer!= null){  //一定要清空播放器資源  
								mediaPlayer.stop();    
								mediaPlayer.release();    
								mediaPlayer = null ;    
				              } 
							//R.raw.zero 為raw文件夾的語音文件 ,以下同上
							mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.zero);
							mediaPlayer.start();
							break;
						case 1:
							if(mediaPlayer!= null){  //一定要清空播放器資源  
								mediaPlayer.stop();    
								mediaPlayer.release();    
								mediaPlayer = null ;    
				              }   
							mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.one);
							mediaPlayer.start();
							break;
						case 2:
							if(mediaPlayer!= null){  //一定要清空播放器資源  
								mediaPlayer.stop();    
								mediaPlayer.release();    
								mediaPlayer = null ;    
				              }   
							mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.two);
							mediaPlayer.start();
							break;
						case 3:
							if(mediaPlayer!= null){  //一定要清空播放器資源  
								mediaPlayer.stop();    
								mediaPlayer.release();    
								mediaPlayer = null ;    
				              }   
							mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.three);
							mediaPlayer.start();
							break;
						case 4:
							if(mediaPlayer!= null){  //一定要清空播放器資源  
								mediaPlayer.stop();    
								mediaPlayer.release();    
								mediaPlayer = null ;    
				              }   
							mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.four);
							mediaPlayer.start();
							break;
						case 5:
							if(mediaPlayer!= null){  //一定要清空播放器資源  
								mediaPlayer.stop();    
								mediaPlayer.release();    
								mediaPlayer = null ;    
				              }   
							mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.five);
							mediaPlayer.start();
							break;
						case 6:
							if(mediaPlayer!= null){  //一定要清空播放器資源  
								mediaPlayer.stop();    
								mediaPlayer.release();    
								mediaPlayer = null ;    
				              }   
							mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.six);
							mediaPlayer.start();
							break;
						case 7:
							if(mediaPlayer!= null){  //一定要清空播放器資源  
								mediaPlayer.stop();    
								mediaPlayer.release();    
								mediaPlayer = null ;    
				              }   
							mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.seven);
							mediaPlayer.start();
							break;
						case 8:
							if(mediaPlayer!= null){  //一定要清空播放器資源  
								mediaPlayer.stop();    
								mediaPlayer.release();    
								mediaPlayer = null ;    
				              }   
							mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.eight);
							mediaPlayer.start();
							break;
						case 9:
							if(mediaPlayer!= null){  //一定要清空播放器資源  
								mediaPlayer.stop();    
								mediaPlayer.release();    
								mediaPlayer = null ;    
				              }   
							mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.nine);
							mediaPlayer.start();
							break;
						default:
							break;
						}
					}
				} else {
					beforeLength = length;
				}
			}
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			length = numInput.getText().length();
		}
	}
    //移除監聽
	public void removeTextChangedListener(TextWatcher watcher) {
		if (mListeners != null) {
			int i = mListeners.indexOf(watcher);
			if (i >= 0) {
				mListeners.remove(i);
			}
		}
		numInput.removeTextChangedListener(watcher);
	}

	@Override
	protected void onDestroy() {
		removeTextChangedListener(watch);
		super.onDestroy();
	}
記錄與分享,讓你我共成長!歡迎查看我的其他博客;我的博客地址:http://blog.csdn.net/caicongyang


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved