2017-05-09 77 views
0

我想獲取加速計傳感器數據並同時播放音樂。當我評論所有與加速度傳感器相關的代碼時,音樂可以發揮得很好。但是,當我沒有對它發表評論時,它不能播放音樂,但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(); 
    } 

} 
+0

您可能希望把'registerListener'調用空檢查後。嘗試註冊到空傳感器將引發異常。 –

回答

0
if (accelerometer != null) { 
    return; 
} 

換句話說:如果有一個加速度計,從onCreate現在返回並跳過剩下的代碼包括,設定按鈕,點擊收聽。

+0

是的,非常感謝,我會盡力做到這一點。 –

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

伎倆,但工作這個代碼我的設備上

+0

哇,這是有效的,非常感謝 –

+0

@FanFan但你明白爲什麼*這個按預期工作,你的原始代碼沒有? –

+0

@EugenPechanec是的,我想我明白這一點。原因是你之前說過的。這種方式只是讓應用程序首先運行按鈕,然後運行空檢查。我不確定我的理解是否正確。最後我使用你的建議,它工作得很好 –