当前位置:操作系统 > 安卓/Android >>

Android通过摇晃手机的频率来控制声音的频率

通过晃动手机的频率来修改播放声音的频率。效果很给力的说。主要通过sensor来算手机摇晃的频率,摇晃的频率越高,播放声音的速度越快。
 
  1 /**
  2  * @author Stay
  3  *        通过摇晃手机的频率来改变声音的速度
  4  */
  5  public class ShakeSound extends Activity implements SensorEventListener,OnClickListener {
  6     private static final float SHAKE_THRESHOLD = 50;
  7     private static final String TAG = "ActivityTest";
  8     private SensorManager manager;
  9     private SoundManager sm;
 10     private long curTime, lastUpdate;
 11     private float last_x, last_y, last_z;
 12     private float x,y,z;
 13     private boolean isPlaying;
 14     private int count = -2;
 15     private float force;
 16     private int audioCount = 0;
 17     private float audioForce;
 18     private Button btn;
 19
 20     @Override
 21     protected void onCreate(Bundle savedInstanceState) {
 22         super.onCreate(savedInstanceState);
 23         setContentView(R.layout.main);
 24         btn = (Button) this.findViewById(R.id.hello);
 25         btn.setOnClickListener(this);
 26         manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
 27         sm = new SoundManager(this);
 28         sm.addSound(1, "heixiu.ogg");
 29     }
 30
 31     @Override
 32     public void onSensorChanged(SensorEvent event) {
 33         curTime = System.currentTimeMillis();
 34         if ((curTime - lastUpdate) > 100) {
 35             count ++ ;
 36             if (count < 0) {
 37                 return;
 38             }
 39             if (count == 0) {
 40                 if (!isPlaying) {
 41                     Log.i(TAG, "sm.play(1, 0, 1.0f);");
 42                     sm.play(1, 0, 1.0f);
 43                 }
 44                 last_x = event.values[SensorManager.DATA_X];
 45                 last_y = event.values[SensorManager.DATA_Y];
 46                 last_z = event.values[SensorManager.DATA_Z];
 47                 return;
 48             }
 49             lastUpdate = curTime;
 50             x = event.values[SensorManager.DATA_X];
 51             y = event.values[SensorManager.DATA_Y];
 52             z = event.values[SensorManager.DATA_Z];
 53             curTime = System.currentTimeMillis();
 54             // 每100毫秒检测一次
 55              float deltaForce =  Math.abs(x + y + z - last_x - last_y - last_z);
 56             force = force + deltaForce;
 57             updateAudioRate(deltaForce);
 58             if (count >= SHAKE_THRESHOLD) {
 59                 Log.i(TAG, "unSensorListener");
 60  //                onShakeCallBack(force / count);     get the score
 61                  unSensorListener();   
 62                 if (isPlaying) {
 63                     sm.stop();
 64                     isPlaying = false;
 65                 }
 66                 count = - 2;
 67                 force = 0;
 68                 return;
 69             }
 70             last_x = x;
 71             last_y = y;
 72             last_z = z;
 73         }
 74     }
 75
 76
 77     @Override
 78     public void onAccuracyChanged(Sensor sensor, int accuracy) {
 79     }
 80
 81     private void updateAudioRate(float force) {
 82         float rat

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,