我是android新手。我想在手机发抖时播放音频文件。我知道加速代码。但是当我编写媒体播放器代码时,我没有从我的设备获得任何输出。
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
getAccelerometer(event);
MediaPlayer mPlayer1 = MediaPlayer.create(null, R.raw.hello);
try {
mPlayer1.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPlayer1.start();
}
}
答案 0 :(得分:0)
使用以下课程进行摇晃听众,
import java.util.List;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class ShakeListener implements SensorEventListener{
private MyShakeListener mOnShakeListener = null;
private SensorManager mSensorManager;
private double mTotalForcePrev;
private double mForceThreshHold = 1.5f;
private List<Sensor> mSensors;
private Sensor mAccelerationSensor;
public ShakeListener(SensorManager sm){
mSensorManager = sm;
mSensors = mSensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
if(mSensors.size() > 0) {
mAccelerationSensor = mSensors.get(0);
mSensorManager.registerListener(this, mAccelerationSensor, SensorManager.SENSOR_DELAY_GAME);
}
}
public void setForceThreshHold(double threshhold){
mForceThreshHold = threshhold;
}
public double getForceThreshHold(){
return mForceThreshHold;
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
public void onSensorChanged(SensorEvent event) {
double totalForce = 0.0f;
totalForce += Math.pow(event.values[0]/SensorManager.GRAVITY_EARTH, 2.0);
totalForce += Math.pow(event.values[1]/SensorManager.GRAVITY_EARTH, 2.0);
totalForce += Math.pow(event.values[2]/SensorManager.GRAVITY_EARTH, 2.0);
totalForce = Math.sqrt(totalForce);
if((totalForce < mForceThreshHold) && (mTotalForcePrev > mForceThreshHold)) {
OnShake();
}
mTotalForcePrev = totalForce;
}
public void setOnShakeListener(MyShakeListener listener)
{
mOnShakeListener = listener;
}
private void OnShake()
{
if(mOnShakeListener!=null)
{
mOnShakeListener.onShake();
}
}
}
public interface MyShakeListener {
public abstract void onShake();
}
并在主类上添加侦听器类,如下所示,
public class MainActivity extends Activity
{
MediaPlayer defaultPlayer;
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.playerscree);
ShakeListener myShake = new ShakeListener((SensorManager)getSystemService(Context.SENSOR_SERVICE));
myShake.setForceThreshHold(1.9);
myShake.setOnShakeListener(new MyShakeListener()
{
@Override
public void onShake()
{
// do ur work here...
defaultPlayer= MediaPlayer.create(getApplicationContext(), R.raw.music);
defaultPlayer.start();
}
});
}
答案 1 :(得分:0)
创建Context
时,您应该提供MediaPlayer
。 E.g。
MediaPlayer.create(getApplicationContext(), R.raw.hello);
此外,建议事先创建一个MediaPlayer。
答案 2 :(得分:0)
像这样修改你的代码。希望这有效。
MediaPlayer mPlayer1 = MediaPlayer.create(null, R.raw.hello);
到
MediaPlayer mPlayer1 = MediaPlayer.create(getApplicationContext(), R.raw.hello);