Pause the background music in android
- 2020-06-15 10:13:03
- OfStack
There are many examples of pausing background music on the Internet, and the most common one is pausing the music by broadcasting the pausing command. In fact, this approach is not reliable, why? Most of the time it doesn't work because other players aren't registered to listen to the broadcast.
There is a good way to manipulate the player's state by triggering a button on the media.
sendMediaButton(context,<span style="font-size:9pt;line-height:1.5;color:#2B91AF;">KeyEvent</span><span style="font-size:9pt;line-height:1.5;">.KEYCODE_MEDIA_PAUSE);</span>
private static void sendMediaButton(Context context, int keyCode) {
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
context.sendOrderedBroadcast(intent,null);
keyEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
context.sendOrderedBroadcast(intent,null);
}
Net friend realizes another kind of method
Simplify and write down the process
{
private Visualizer visualizer = null;// Spectrum tester
private byte [] mRawVizData;// Spectrum of the container
// instantiation Visualizer object
visualizer = new Visualizer(0);
mRawVizData = new byte[128];
// Initialization of an object
if(visualizer != null)
{
if (visualizer.getEnabled()) {
visualizer.setEnabled(false);
}
visualizer.setCaptureSize(mRawVizData.length);//1 It has to be in the spectrum false State use
visualizer.setEnabled(true);// Open spectrum acquisition
}
I'm just going to loop through 1 thread to get the spectrum information, and I'm just going to post it
int status = Visualizer.ERROR;
if(visualizer != null)
{
// Music spectrum acquisition
status = visualizer.getFft(mRawVizData);// Acquisition waveform
if(status != Visualizer.SUCCESS)
{
Log.i("answer", "getWaveFail");
}
else
{
int j = 0;
for(int i = 0; i < 128; i++)
{
if(mRawVizData[i] == 0)
{
j++;
}
}
Log.i("answer", "getWave j = " + j);
}
}
}