Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to make the phone vibrate to the music playing

I am new to Android development. I want to make an app that is essentially a music player but the phone can vibrate to the beat of the music playing. I remember the earliest Nokia phones had this feature, I want to recreate this for fun.

My question: is this possible as an Android app? If so, what are the general approaches? Do I have to analyse the sound patterns of the song then alter the vibration intensity?

Thanks!

like image 800
naitud Avatar asked Mar 11 '15 08:03

naitud


People also ask

How do I make my phone vibrate for music?

Tap Settings > Sounds and vibration. Tap Sound mode > Vibrate.

Why does my phone vibrate when music is playing?

Go into Settings>Accessibility>Hearing and turn off Auto Haptic. The phone vibrates when audio is playing even through bluetooth.

Can I make volume changes using vibration?

To create sound, you need to make vibrations. We can change the size of these vibrations to make them bigger or smaller. This changes the loudness or volume of the sound. The size of the vibration (also called amplitude) determines the volume.

How do I separate ringtone and notification volume Android?

The ringtone and notification sound volume can't be adjust separately as the Android operating system uses a single volume control for both. If you've increased the ringtone volume and the notification sound seems too loud for you, consider muting the notification sound.


1 Answers

I got interested with this question and after deep research I figured out how to do that. So here we go.

import android.media.MediaPlayer;
import android.os.Vibrator;

private Vibrator vibrator;
private MediaPlayer player;
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    player = MediaPlayer.create(this, R.raw.music);
    vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    player.start();

    //HERE WE WILL START VIBRATION

    Button button = (Button) findViewById(R.id.btn);
    button.setOnClickListener(this);
}

public void onClick(View v){
    player.stop();
    vibrator.cancel();
}

This is general approach for playing music and now work tight with the method vibrate();. From all of constructors of class Vibrator we need this one:

public void vibrate (long milliseconds, AudioAttributes attributes);

It was added in API 21 As first parameter we can pass the duration of the song and this duration we can get this way (source):

String mediaPath = Uri.parse("android.resource://<your-package-name>/raw/filename").getPath();
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(mediaPath);
String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);

As second parameter we need to create AudioAttributes with the help of AudioAttributes.Builder:

vibrator.vibrate(duration, new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build());

Please, NOTE: I haven't tried it. But the doc said it should works fine. Let me know if that is completed way. Best regards.

P.S. Don't forget permission:

<uses-permission android:name="android.permission.VIBRATE" />
like image 151
Yurets Avatar answered Sep 29 '22 16:09

Yurets