• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Playing sound on button click?

Grendizer

Newbie
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:
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DialPad extends Activity implements OnClickListener {
    
    MediaPlayer mp;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button one = (Button) findViewById(R.id.btnOne);
        one.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mp = MediaPlayer.create(this, R.raw.mamacita_one);
            }
        });
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent ev) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_0:
            mp = MediaPlayer.create(this, R.raw.mamacita_zero);
            mp.start();
            return true;
        }

        return super.onKeyDown(keyCode, ev);
    }
}
 
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:
final Context mContext = this;
and pass it as your Context in your OnClickListener function Create()
 
Hey

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

PHP:
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

public class DialPad extends Activity {
    
    MediaPlayer mp;
    final Context mContext = this;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button one = (Button) findViewById(R.id.btnOne);
        one.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mp = MediaPlayer.create(mContext, R.raw.mamacita_one);
                mp.start();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent ev) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_0:
            mp = MediaPlayer.create(this, R.raw.mamacita_zero);
            mp.start();
            return true;
        }

        return super.onKeyDown(keyCode, ev);
    }
}
 
Hey

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

PHP:
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

public class DialPad extends Activity {
    
    MediaPlayer mp;
    final Context mContext = this;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button one = (Button) findViewById(R.id.btnOne);
        one.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mp = MediaPlayer.create(getApplicationContext(), R.raw.mamacita_one);
                mp.start();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent ev) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_0:
            mp = MediaPlayer.create(this, R.raw.mamacita_zero);
            mp.start();
            return true;
        }

        return super.onKeyDown(keyCode, ev);
    }
}

Look at my change above. Use getApplicationContext() instead of mContext.
 
The error code shows up when i start the emulator:

"The application Dial Pad (process com.ovning4.dialpad) has stopped unexpectadly. Please try again."
 
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!
 
Back
Top Bottom