使用加速计传感器时无法播放音乐

时间:2017-05-09 22:13:17

标签: android

我想获得加速计传感器数据并同时播放音乐。当我评论与加速计传感器相关的所有代码时,音乐可以很好地播放。然而,当我没有评论它时,它不能再播放音乐,但是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();
    }

}

2 个答案:

答案 0 :(得分:0)

if (accelerometer != null) {
    return;
}

换句话说:如果有加速度计,请立即从onCreate返回并跳过剩余的代码,包括设置按钮点击监听器的代码。

答案 1 :(得分:0)

将此代码放在setContentView()mSensormanager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

之上
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);
    }
});

技巧但在我的设备上工作