Modifying Vanilla Music Player for Android (Part 2)

  • Tutorial
In the last article, we examined how you can add the ability to switch tracks using the volume keys to the open-source player Vanilla Music , if the device is in your pocket (for example). In this article, we will continue the modification with the main idea for which the following idea served - how can I switch tracks without touching the smartphone, without unlocking it - in general with minimal effort.

As a result, it was decided to use information from the proximity sensor (Proximity sensor) . The essence of the idea is to switch the track when a proximity signal from the sensor appears.

So, let's proceed to the modification. To work with the proximity sensor, you must enable its “listening” in the application. This was done in a previous article , but just in case, I will give the configuration code:

/**
	 * Setup the accelerometer.
	 */privatevoidsetupSensor(){
		if (mSensorManager == null)
			mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
		if (mShakeAction == Action.Nothing)
		{
			if (mSensorManager != null)
				mSensorManager.unregisterListener(this);
		}
		if(enable_defer_stop)
			 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
		mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY),SensorManager.SENSOR_DELAY_NORMAL);
}

Now you need to track the state of the proximity sensor. Like last time, we turn to the public void onSensorChanged (SensorEvent se) method , whose role (judging by its name) is called up when the state of registered sensors changes. In the last article, we already determined which sensor produced the changes, but I will also provide the tracking code here:

@OverridepublicvoidonSensorChanged(SensorEvent se){
		if (se.sensor.getType() == Sensor.TYPE_PROXIMITY)
		{
			if( se.values[0] == 0) 
			 isproximity = true;
			else 
			 isproximity = false;
  }		
  ....

Isproximity variable - a member of the PlayBackService class stores the last state received from the sensor (true or false). Now you need to compare it with the previous state - to do this, add another boolean variable prev_isproximity , by default set prev_isproximity = false and add the setting of this variable to the OnSensorChanged function

@OverridepublicvoidonSensorChanged(SensorEvent se){
		if (se.sensor.getType() == Sensor.TYPE_PROXIMITY)
		{
			if( se.values[0] == 0) 
			 isproximity = true;
			else 
			 isproximity = false;	
   prev_isproximity = isproximity;
   ....

We turn to the main code of our functional - switching tracks by the condition of changing the approximation. Switching should occur when the following expressions are true (isproximity! = Prev_isproximity) and (isproximity == true) . In order for the switching to work with a certain object delay above the proximity sensor (for example, 1 second), it is also necessary to add a definition of the difference between the current time and the previous time the information from the sensor changed. To do this, declare the variable long mLastProximityChangeTime in the class itself, which will store the time of the last change of information from the sensor. Based on the difference of the current time of changing information from the sensor and the time of the latter, we determine their difference, which we use to perform the actions:

@OverridepublicvoidonSensorChanged(SensorEvent se){
		if (se.sensor.getType() == Sensor.TYPE_PROXIMITY)
		{
			if( se.values[0] == 0)
			 isproximity = true;
			else 
			 isproximity = false;
			// переключение при помощи нажатия на сенсор приближенияif(enable_proximity_track_next) // boolean переменная для включения опции
			{
			 if(isproximity!= prev_isproximity)
				{
					long now = SystemClock.elapsedRealtime(); // определяем текущий момент времениif (now - mLastProximityChangeTime > 1000 ) // если датчик перекрыт в течении 1 секунды
					{
						if(isproximity) // если датчик показывает приближение
						{
							performAction(Action.NextSong, null); // переключение трека на следующий
						}
					}
				}
			}
			prev_isproximity = isproximity;
			mLastProximityChangeTime = SystemClock.elapsedRealtime();;
		}
else 
 ...

As a result, we got an audio player that allows you to switch tracks simply by holding your hand over the top of the smartphone (at the location of the proximity sensor), while spending quite a bit of time. This is again the principle of Open Source shows itself from the best side! You can download the modified project source code in the github repository .

Only registered users can participate in the survey. Please come in.

Have you developed your project based on another opensource project?


Also popular now: