1. Download our Official Android App: Forums for Android!

Apps Headphone pause button event capture

Discussion in 'Android Development' started by GIR, May 31, 2011.

  1. GIR

    GIR Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    18
    Posts:
    96
    Joined:
    Feb 14, 2010

    Feb 14, 2010
    96
    10
    18
    Classified.
    UK
    Hello everyone, I've had a full-on Android day today :D

    I got sidetracked looking to find code examples about reverse geolocation... and found some details about the play/pause button on the headphones.

    According to some poking around with a spare headset and a multimeter I discovered that there is a means of captureing data from external devices via the TRRS plug/jack.

    I've discovered that the standard media player responds to 4 states:
    1. Nothing, no presses, voltage on ring2 connector is 2.6 volts.
    2. Forward/next track, voltage on ring 2 is around 0.5 volts.
    3. Back/previous track, voltage is around 0.18 ~ 0.20 volts.
    4. Play/pause, ring 2 is at 0 volts.

    I have made a circuit that replicates these voltages, simply by switching output pins to resistors and i'd like to capture these 4 states as a simple data logger.

    Basically the end goal is to update an old weather monitoring project so the data on the eeprom is simply sent using the headphone socket connection so save me from having to dismantle the weather proof housing, extract the chip to go into the reader, and all the reverse.

    Has anyone got any code samples that could guide me in the right direction.

    As you may guess im more at home with a soldering iron and PIC assembly code than Java, but I'm finding more and more reasons to keep on the learning curve :).

    Thanks for any help,
    GIR
     

    Advertisement

  2. GIR

    GIR Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    18
    Posts:
    96
    Joined:
    Feb 14, 2010

    Feb 14, 2010
    96
    10
    18
    Classified.
    UK
    UPDATE

    I was tipped off about some this site and it looks like the way to go for me. :)

    However I am stuck at the first hurdle and being new to this I am really really stumped. :eek:

    The code block of
    Code (Text):
    1.  
    2. MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
    3.  IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
    4. mediaFilter.setPriority(MEDIA_BUTTON_INTENT_EMPIRICAL_PRIORITY_VALUE);
    5. registerReceiver(mMediaButtonReceiver, mediaFilter);
    6.  
    .. is throwing up some errors in eclipse, and despite all my efforts so far I cannot correct them. :thinking:
    The errors are:
    Code (Text):
    1.  
    2. Syntax error on token "MEDIA_BUTTON_INTENT_EMPIRICAL_PRIORITY_VALUE", VariableDeclaratorId expected after this token
    +
    Code (Text):
    1. mMediaButtonReceiver cannot be resolved to a type
    +
    Code (Text):
    1. Return type for the method is missing
    + 2x
    Code (Text):
    1. Syntax error on token ";", delete this token
    I know Ii'm in over my head with making the jump from a simple soundboard to this, but I have to try eh?

    Many thanks for any help,
    GIR
     
  3. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    It sounds like you are missing a import statement for the MediaButtonIntentReceiver
     
  4. GIR

    GIR Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    18
    Posts:
    96
    Joined:
    Feb 14, 2010

    Feb 14, 2010
    96
    10
    18
    Classified.
    UK
    Thanks for getting back to me miXer.

    I found the following code samples:

    1. From Gaunt Face - Matthew Gaunt | Using Android Headset Buttons (Earphone Buttons)
    Code (Text):
    1.  
    2. HardButtonReceiver buttonReceiver = new HardButtonReceiver();
    3. IntentFilter iF = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
    4. iF.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
    5. registerReceiver(buttonReceiver, iF);
    6.  
    2. From [CSDb] - User Forums - Android SID/MOD/etc player
    Code (Text):
    1. IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
    2.  intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);                
    3. service.registerReceiver(headsetButtonReceiver, intentFilter);
    4.  
    The pair of these examples look very similar, so I'm wondering if I'm using the incorrect API, as currently Im using 2.1.

    My code looks like this:
    Code (Text):
    1.  
    2. package com.nds.PausebtnPress;
    3.  
    4. import android.app.Activity;
    5. import android.content.BroadcastReceiver;
    6. import android.content.Context;
    7. import android.content.Intent;
    8. import android.content.IntentFilter;
    9. import android.os.Bundle;
    10. import android.util.Log;
    11. import android.view.KeyEvent;
    12. import android.widget.TextView;
    13.  
    14. public class PausebtnPress extends Activity {
    15.     /** Called when the activity is first created.
    16.      *
    17.      * MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
    18.      * IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
    19.      * mediaFilter.setPriority(MEDIA_BUTTON_INTENT_EMPIRICAL_PRIORITY_VALUE);
    20.      * registerReceiver(mMediaButtonReceiver, mediaFilter);
    21. */
    22.    
    23.     HardButtonReceiver buttonReceiver = new HardButtonReceiver();{
    24.     IntentFilter iF = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
    25.     //SYSTEM_HIGH_PRIORITY
    26.     iF.setPriority(0);
    27.     registerReceiver(buttonReceiver, iF);}
    28.    
    29.     // do something
    30.    
    31.     //text = "The PAUSE button is pressed!";
    32.    
    33.    
    34.        
    35.     public String text = null;
    36.    
    37.     @Override
    38.     public void onCreate(Bundle savedInstanceState) {
    39.         super.onCreate(savedInstanceState);
    40.         setContentView(R.layout.main);
    41.        
    42.         // need to setup to change the textview
    43.         TextView tv = (TextView)this.findViewById(R.id.textView1);
    44.        
    45.         tv.setText(text);
    46.        
    47.  
    48.  
    49.      
    50.     }
    51.     public class HardButtonReceiver extends BroadcastReceiver
    52.     {
    53.     @Override
    54.     public void onReceive(Context context, Intent intent)
    55.     {
    56.     Log.v("TestApp", "Button press received");
    57.     abortBroadcast();
    58.     KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    59.     text ="Button pressed";
    60.  
    61.     if(key.getAction() == KeyEvent.ACTION_UP)
    62.     {
    63.     int keycode = key.getKeyCode();
    64.     if(keycode == KeyEvent.KEYCODE_MEDIA_NEXT)
    65.     {
    66.     Log.d("TestApp", "Next Pressed");
    67.     }
    68.     else if(keycode == KeyEvent.KEYCODE_MEDIA_PREVIOUS)
    69.     {
    70.     Log.d("TestApp", "Previous pressed");
    71.     }
    72.     else if(keycode == KeyEvent.KEYCODE_HEADSETHOOK)
    73.     {
    74.     Log.d("TestApp", "Head Set Hook pressed");
    75.     }
    76.     }
    77.     }
    78.     }
    79. }
    80.  
    Thanks for any help,

    The code above now compiles with out any errors, BUT when run on my phone it instantly force-closes!
    GIR
     
  5. GIR

    GIR Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    18
    Posts:
    96
    Joined:
    Feb 14, 2010

    Feb 14, 2010
    96
    10
    18
    Classified.
    UK

Share This Page

Loading...