Pause background music in Android _android

Source: Internet
Author: User

There are many examples of suspended background music on the internet, most of which are broadcast to suspend music according to a paused instruction. In fact, this way is very unreliable, why? Because other players may not register to listen for that broadcast, most of them don't work.

Here's a more reliable way to manipulate the player state by triggering a button on the media.

Copy Code code as follows:

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>

Copy Code code as follows:

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);
}

Another way for a user to realize

Simple to write down the process

{
 Private Visualizer visualizer = null;//spectrum tester
 Private byte [] mrawvizdata;//Spectrum container


 //Instantiated Visualizer object
 Visualizer = new Visualizer (0); 
 Mrawvizdata = new byte[128];

 The initialization
 if (visualizer!= null) of an object
 {
  if (visualizer.getenabled ()) {
    visualizer.setenabled (false);

   }

    visualizer.setcapturesize (mrawvizdata.length);/Be sure to use
    visualizer.setenabled (true) in the spectral false state; Open Spectrum Access

 }

Here's a thread to loop through the spectrum, and I'll just post the fetch.

int status = Visualizer.error;

 if (visualizer!= null)
 {

  //Music spectrum get
  status = Visualizer.getfft (Mrawvizdata);//Get Waveform graph

  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);}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.