单击imageview /按钮后播放声音

时间:2014-05-29 13:32:05

标签: android click

我正试图在图像上播放点击声(ImageView / Button)点击,我看到example here。我也试过用这个:

profile_pick.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
            Intent i=new Intent(getApplicationContext(),Profile_pic.class);
            startActivity(i);
            overridePendingTransition(R.anim.animation,R.anim.animation2);
        }
    });

但它不起作用。谁能建议怎么做?

(添加原始声音不是一个好主意,所以我更喜欢使用系统声音...)

3 个答案:

答案 0 :(得分:3)

android:soundEffectsEnabled="true"添加到profile_pick。文档说明

  

只有在启用了声音效果时才会播放声音效果   用户,isSoundEffectsEnabled()为true。

因此两个条件都是强制性的。 Here您可以找到文档

答案 1 :(得分:1)

profile_pick.setOnClickListener(new OnClickListener() {

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

        AudioManager audioManager = 
        (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        audioManager.playSoundEffect(SoundEffectConstants.CLICK); 
        Intent i=new Intent(getApplicationContext(),Profile_pic.class);
        startActivity(i);
        overridePendingTransition(R.anim.animation,R.anim.animation2);
    }
});

答案 2 :(得分:0)

首先,您需要将语句放在一个块中,在本例中是onCreate方法。

之后,将按钮初始化为变量1,然后使用变量零并将其onClickListener设置为不完整的onClickListener。将变量1用于setOnClickListener。

最后,将逻辑放在onClick中播放声音。

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BasicScreenActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_screen);

        Button one = (Button)this.findViewById(R.id.button1);
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho);
        one.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                mp.start();
            }
        });     
    }
}
相关问题