Recently, I have used native Android and IOS in the project, so I will record what I have learned recently~
One of the requirements in the project is to control the system music play/pause on the mobile phone through the bracelet or to switch from the previous song to the next song. Here, the AudioManager in Android is used to realize the management of system music
First get the AudioManager:
AudioManager audioManager = ((AudioManager) getSystemService(Context.AUDIO_SERVICE));
After getting the AudioManager class, everything will be easy. First of all, the first requirement is to get whether the music is currently playing
Just use the isMusicActive() method in AudioManager, which will return a Boolean value, true means that there is sound currently playing, and false means there is no sound
boolean isMusicActive = audioManager.isMusicActive();
Next is to pause or start the music
Here you can use AudioManager to send KeyEvent button events
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(downEvent);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(upEvent);
Next is the next track, the same way as pausing/starting music:
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(downEvent);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(upEvent);
Previous song:
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(downEvent);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(upEvent);
OK, now that we have implemented the basic music control, let’s write an Activity to test it. Here I simply use three buttons to bind the event to test:
MainActivity.java:
package com.shine.music;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AudioManager audioManager = ((AudioManager) getSystemService(Context.AUDIO_SERVICE));
boolean isMusicActive = audioManager.isMusicActive();
Log.e("shine",isMusicActive + "");
}
public void lastMusic(View view) {
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(downEvent);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(upEvent);
}
public void playMusicLocal(View view) {
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(downEvent);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(upEvent);
}
public void nextMusicLocal(View view) {
KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT);
KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(downEvent);
((AudioManager) getSystemService(Context.AUDIO_SERVICE)).dispatchMediaKeyEvent(upEvent);
}
}
It's ok after testing~
Full code: