简体   繁体   中英

can anyone tell me how do I start mp3?

I am making a project ,I need to start mp3 or any loud sound automatically when the toast "Fall Detected" appears for 20 second.

 public void onSensorChanged(SensorEvent event) 
 {
     if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) 
     {
         long curTime = System.currentTimeMillis();
         if ((curTime - mLastShakeTime) > MIN_TIME_BETWEEN_SHAKES_MILLISECS) 
         {
             float x = event.values[0];
             float y = event.values[1];
             float z = event.values[2];

             double acceleration = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)) - SensorManager.GRAVITY_EARTH;

             Log.d("mySensor", "Acceleration is " + acceleration + "m/s^2");

             if (acceleration < -9.00f && acceleration> -15.00f ) 
             {
                 mLastShakeTime = curTime;
                 Toast.makeText(getApplicationContext(), "FALL DETECTED",
                 Toast.LENGTH_LONG).show();
             }
         }
     }
 }

这是设置警报的链接http://developer.android.com/shareables/training/Scheduler.zip在显示吐司之后,调用setAlarm(context)方法

Given your clarifications to the original question it sounds like you want to play a sound. In which case you want something like:

final MediaPlayer player = MediaPlayer.create(this, R.raw.alarm);
player.start();

R.raw.alarm is the resource for the file containing the sound you want to play.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM