• 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
Mar 14, 2010
10
0
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);
    }
}
 
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);
    }
}
 
Upvote 0
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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones