我想獲取加速計傳感器數據並同時播放音樂。當我評論所有與加速度傳感器相關的代碼時,音樂可以發揮得很好。但是,當我沒有對它發表評論時,它不能播放音樂,但logcat中沒有錯誤,我可以很好地獲取傳感器數據。我不知道爲什麼會發生這種情況,我非常感謝您的幫助!使用加速計傳感器時無法播放音樂
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class MUSIC extends AppCompatActivity implements SensorEventListener, MediaPlayer.OnPreparedListener {
private Button startButton;
private SensorManager mSensormanager;
private Sensor accelerometer;
private final float NOISE=(float)2.0;
private boolean initialization;
private float mLastX, mLastY, mLastZ;
private static final String TAG = "sensor";
final MediaPlayer mediaPlayer=new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
mSensormanager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
accelerometer=mSensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensormanager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
if(accelerometer!=null){
return;
}else{
Toast.makeText(getApplicationContext(),"Sorry,there are no accelerometers on your device",Toast.LENGTH_LONG).show();
}
startButton=(Button)findViewById(R.id.button3);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Prepareplay(mediaPlayer,Uri.parse("android.resource://"+getPackageName()+ "/raw/test1"));
onPrepared(mediaPlayer);
}
});
}
@Override
public void onSensorChanged(SensorEvent event) {
float x=event.values[0];
float y=event.values[1];
float z=event.values[2];
if(initialization==false){
mLastX=x;
mLastY=y;
mLastZ=z;
initialization=true;
}else {
float deltaX = Math.abs(mLastX - x);
float deltaY = Math.abs(mLastY - y);
float deltaZ = Math.abs(mLastZ - z);
Log.i(TAG,"\n x "+deltaX);
Log.i(TAG,"\n y "+deltaY);
Log.i(TAG,"\n z "+deltaZ);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onResume(){
super.onResume();
mSensormanager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause(){
super.onPause();
mSensormanager.unregisterListener(this);
}
public void Prepareplay(MediaPlayer player, Uri uri){
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
player.setDataSource(getApplicationContext(), uri);
} catch (IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
public void onPrepared(MediaPlayer player){
player.start();
}
public void pause(MediaPlayer player){
player.pause();
}
}
您可能希望把'registerListener'調用空檢查後。嘗試註冊到空傳感器將引發異常。 –