上一篇写了如何在线匹配歌词,http://blog.csdn.net/huweigoodboy/article/details/39878063,现在来讲讲摇一摇功能开发。
同样用了一个Service去实现摇一摇。
ShakeListener继承自SensorEventListener,当加速度感应器感受到重力变化,就去通知onShakeListener调用onShake(),
在震动的同时随机一首歌。
在这里需要设置一个速度阈值和时间间隔,控制一定的时间间隔才能触发第二次震动,还有感受速度太小也不去onShake()。
1)得到sensorManager。
sensorManager=(SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
2)得到sensor
sensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
3)注册感应监听
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
package com.huwei.sweetmusicplayer.ui.listeners; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; public class ShakeListener implements SensorEventListener { //速度阈值 private static final int SPEED_SHAKEHOLD=3000; //检测时间间隔 private static final int UPTATE_INTERVAL_TIME = 70; //传感器管理器 private SensorManager sensorManager; //传感器 private Sensor sensor; //重力感应监听器 private OnShakeListener onShakeListener; // 上下文 private Context mContext; // 手机上一个位置时重力感应坐标 private float lastX; private float lastY; private float lastZ; // 上次检测时间 private long lastUpdateTime; public ShakeListener(Context mContext) { super(); this.mContext = mContext; start(); } public void start(){ sensorManager=(SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); if(sensorManager!=null){ //获得加速度感应器 sensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); } if(sensor!=null){ //注册加速器,并且设置速率 sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); } } // 设置重力感应监听器 public void setOnShakeListener(OnShakeListener listener) { onShakeListener = listener; } public void stop(){ sensorManager.unregisterListener(this); } //重力感应到变化 @Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub long currentUpdateTime=System.currentTimeMillis(); long timeInterval=currentUpdateTime-lastUpdateTime; if(timeInterval<UPTATE_INTERVAL_TIME){ return; } lastUpdateTime=currentUpdateTime; float x=event.values[0]; float y=event.values[1]; float z=event.values[2]; float deltaX=x-lastX; float deltaY=y-lastY; float deltaZ=z-lastZ; lastX=x; lastY=y; lastZ=z; double speed=Math.sqrt(deltaX*deltaX+deltaY*deltaY*deltaZ*deltaZ)/timeInterval*10000; if(speed>SPEED_SHAKEHOLD){ onShakeListener.onShake(); } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } // 摇晃监听接口 public interface OnShakeListener { public void onShake(); } }
得到Vibrator对象
vibrator=(Vibrator) getBaseContext().getSystemService(Context.VIBRATOR_SERVICE);
震动方法有两种
vibrator.vibrate(500);震动多少毫秒
或者设置一个震动周期的数组
vibrator.vibrate(new long[]{100,200,100,200}, -1); //停止,震动,停止,震动
-1表示重复次数为不重复
package com.huwei.sweetmusicplayer.services; import com.huwei.sweetmusicplayer.datamanager.MusicManager; import com.huwei.sweetmusicplayer.ui.listeners.ShakeListener; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.os.Vibrator; import android.util.Log; public class ShakeService extends Service { private static final String TAG="ShakeService"; private ShakeListener mShakeListener; private Vibrator vibrator; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); vibrator=(Vibrator) getBaseContext().getSystemService(Context.VIBRATOR_SERVICE); mShakeListener=new ShakeListener(getBaseContext()); mShakeListener.setOnShakeListener(new ShakeListener.OnShakeListener() { @Override public void onShake() { // TODO Auto-generated method stub mShakeListener.stop(); startVibrator(); //vibrator.cancel(); mShakeListener.start(); } } ); } //震动函数 void startVibrator(){ Log.i(TAG,"shake"); vibrator.vibrate(500); MusicManager.getInstance().random_a_song(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub mShakeListener.start(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); mShakeListener.stop(); } }
android音乐播放器开发 SweetMusicPlayer 摇一摇换歌
原文地址:http://blog.csdn.net/huweigoodboy/article/details/39880779