实例11--播放按键声音

阅读更多

  实例11播放按键声音,在java me中有playTone的方法,可以指定一些特定的频率播放,在android中,我没找到相应的方法,所以就用了另一种方法,但是播放出来的生硬不是音乐的音符,只是其他的一些声音,下面给出源代码。因为简单,也没有涉及到界面,所以就不给出效果图,也没有MainView,就单单一个acitivity.

package com.wjh.test_tone;

import android.app.Activity;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.view.KeyEvent;

public class TestTone extends Activity {
    /** Called when the activity is first created. */
	ToneGenerator tonePlayer = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tonePlayer = new ToneGenerator(AudioManager.STREAM_MUSIC, 70);
    }
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch(keyCode)
		{
		case KeyEvent.KEYCODE_0:
			tonePlayer.startTone(ToneGenerator.TONE_DTMF_0,100);
			break;
		case KeyEvent.KEYCODE_1:
			tonePlayer.startTone(ToneGenerator.TONE_DTMF_1,100);
			break;
		case KeyEvent.KEYCODE_2:
			tonePlayer.startTone(ToneGenerator.TONE_DTMF_2,100);
			break;
		case KeyEvent.KEYCODE_3:
			tonePlayer.startTone(ToneGenerator.TONE_DTMF_3,100);
			break;
		case KeyEvent.KEYCODE_4:
			tonePlayer.startTone(ToneGenerator.TONE_DTMF_4,100);
			break;
		case KeyEvent.KEYCODE_5:
			tonePlayer.startTone(ToneGenerator.TONE_DTMF_5,100);
			break;
		case KeyEvent.KEYCODE_6:
				tonePlayer.startTone(ToneGenerator.TONE_DTMF_6,100);
				break;
		case KeyEvent.KEYCODE_7:
			tonePlayer.startTone(ToneGenerator.TONE_DTMF_7,100);
			break;
		case KeyEvent.KEYCODE_8:
			tonePlayer.startTone(ToneGenerator.TONE_DTMF_8,100);
			break;
		case KeyEvent.KEYCODE_9:
			tonePlayer.startTone(ToneGenerator.TONE_DTMF_9,100);
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
	
    
    
}

 在想playtone的时候,我在csdn发问了,fontlose回答了一种方法,我觉得还不错,有发出那7个声音,下面给出他提供的方法源码:

  private final int    duration    = 1; // seconds
        private final int    sampleRate  = 8000;
        private final int    numSamples  = duration * sampleRate;
        private final double sample[] = new double[numSamples];
        private final double freqOfTone[] ={262, 294,330,349,392,440,494,524}; // do,rui,mi,fa,so,la,xi,dou的频率
        private final byte   generatedSnd[] = new byte[2 * numSamples];
            private int          freqCnt=0;

  
  class PlayThread extends Thread
  {
    private int  playfreq;//播放频率 
     public PlayThread(int cnt)
     {
        playfreq=cnt%8;
     }
    
    
    @Override
    public void run() {
        // TODO Auto-generated method stub

          for (int i = 0; i < numSamples; ++i) {
              sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/freqOfTone[playfreq]));
          }
          int idx = 0;
          for (final double dVal : sample) {
              // scale to maximum amplitude
              final short val = (short) ((dVal * 32767));
              // in 16 bit wav PCM, first byte is the low order byte
              generatedSnd[idx++] = (byte) (val & 0x00ff);
              generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
          }
        
        
         final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                  sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                  AudioFormat.ENCODING_PCM_16BIT, numSamples,
                  AudioTrack.MODE_STATIC);
         audioTrack.write(generatedSnd, 0, generatedSnd.length);
         audioTrack.play();
          super.run();
    }      
  }  
 new PlayThread(0).start();  //0播发duo  1播放rui 2播放mi ......
 

 apk文件(将后缀改为 apk):Demon_11.zip

源代码文件:Demon_11.rar

  • Demon_11.zip (13.3 KB)
  • 下载次数: 90
  • Demon_11.rar (37.4 KB)
  • 下载次数: 138

你可能感兴趣的:(Android,音乐,OS,thread)