1. Check out our app, Forums for Android! Download and leave feedback here!

Playing sound on button click?

Discussion in 'Application Development' started by Grendizer, May 2, 2010.

  1. Grendizer

    Grendizer Member
    Thread Starter
    15

    Mar 14, 2010
    10
    0
    15
    Hey all,

    I'm trying to play a soundclip when ever a user clicks an imagebutton on the screen.
    It works fine on the "physical keyboard" of the phone but I can't seem to get it to work when a user clicks on the screen.

    I get the following message:
    The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (new View.OnClickListener(){}, int)

    PHP:
    1.  
    2. import android.app.Activity;
    3. import android.media.MediaPlayer;
    4. import android.os.Bundle;
    5. import android.view.KeyEvent;
    6. import android.view.View;
    7. import android.view.View.OnClickListener;
    8. import android.widget.Button;
    9.  
    10. public class DialPad extends Activity implements OnClickListener {
    11.    
    12.     MediaPlayer mp;
    13.    
    14.     /** Called when the activity is first created. */
    15.     @Override
    16.     public void onCreate(Bundle savedInstanceState) {
    17.         super.onCreate(savedInstanceState);
    18.         setContentView(R.layout.main);
    19.        
    20.         Button one = (Button) findViewById(R.id.btnOne);
    21.         one.setOnClickListener(new OnClickListener() {
    22.             public void onClick(View v) {
    23.                 mp = MediaPlayer.create(this, R.raw.mamacita_one);
    24.             }
    25.         });
    26.     }
    27.  
    28.     @Override
    29.     public void onClick(View v) {
    30.         // TODO Auto-generated method stub
    31.     }
    32.  
    33.     @Override
    34.     public boolean onKeyDown(int keyCode, KeyEvent ev) {
    35.         switch (keyCode) {
    36.         case KeyEvent.KEYCODE_0:
    37.             mp = MediaPlayer.create(this, R.raw.mamacita_zero);
    38.             mp.start();
    39.             return true;
    40.         }
    41.  
    42.         return super.onKeyDown(keyCode, ev);
    43.     }
    44. }
     

    Advertisement

  2. Boogs

    Boogs Well-Known Member
    18

    Apr 6, 2010
    85
    16
    18
    I believe your "this" in your OnClickListener is referring to your listener itself. Try passing the Context of your main activity. I recommend making a global variable:
    Code (Text):
    1. final Context mContext = this;
    and pass it as your Context in your OnClickListener function Create()
     
  3. fabiocberg

    fabiocberg Active Member
    16

    Apr 7, 2010
    27
    1
    16
    Programmer (Coordinator)
    Brazil
    Try to use getApplicationContext() instead of 'this'.
     
  4. Grendizer

    Grendizer Member
    Thread Starter
    15

    Mar 14, 2010
    10
    0
    15
    Hey

    I'm still not getting it to work.
    Here's the code:

    PHP:
    1. import android.app.Activity;
    2. import android.content.Context;
    3. import android.media.MediaPlayer;
    4. import android.os.Bundle;
    5. import android.view.KeyEvent;
    6. import android.view.View;
    7. import android.widget.Button;
    8.  
    9. public class DialPad extends Activity {
    10.    
    11.     MediaPlayer mp;
    12.     final Context mContext = this;
    13.    
    14.     /** Called when the activity is first created. */
    15.     @Override
    16.     public void onCreate(Bundle savedInstanceState) {
    17.         super.onCreate(savedInstanceState);
    18.         setContentView(R.layout.main);
    19.        
    20.         Button one = (Button) findViewById(R.id.btnOne);
    21.         one.setOnClickListener(new View.OnClickListener() {
    22.             public void onClick(View v) {
    23.                 mp = MediaPlayer.create(mContext, R.raw.mamacita_one);
    24.                 mp.start();
    25.             }
    26.         });
    27.     }
    28.  
    29.     @Override
    30.     public boolean onKeyDown(int keyCode, KeyEvent ev) {
    31.         switch (keyCode) {
    32.         case KeyEvent.KEYCODE_0:
    33.             mp = MediaPlayer.create(this, R.raw.mamacita_zero);
    34.             mp.start();
    35.             return true;
    36.         }
    37.  
    38.         return super.onKeyDown(keyCode, ev);
    39.     }
    40. }
     
  5. fabiocberg

    fabiocberg Active Member
    16

    Apr 7, 2010
    27
    1
    16
    Programmer (Coordinator)
    Brazil
    Look at my change above. Use getApplicationContext() instead of mContext.
     
  6. Grendizer

    Grendizer Member
    Thread Starter
    15

    Mar 14, 2010
    10
    0
    15
    Hi,

    Thanks for your reply, but i'm still getting an error message with getApplicationContext() saying "Force close"...very strange...
     
  7. fabiocberg

    fabiocberg Active Member
    16

    Apr 7, 2010
    27
    1
    16
    Programmer (Coordinator)
    Brazil
    Please, paste here the error that is shown in logcat.
     
  8. Grendizer

    Grendizer Member
    Thread Starter
    15

    Mar 14, 2010
    10
    0
    15
    The error code shows up when i start the emulator:

    "The application Dial Pad (process com.ovning4.dialpad) has stopped unexpectadly. Please try again."
     
  9. Grendizer

    Grendizer Member
    Thread Starter
    15

    Mar 14, 2010
    10
    0
    15
    Ahh its my fault, I have been using ImageButtons and in the java code I use Button to find a view by id...:mad:

    Thanks for your help anyways!
     

Share This Page

Loading...