1

いくつかの画像を含むグリッドビューを使用しています。グリッドビューの各画像をクリックしたときに効果音を追加したい。res/raw フォルダーに画像を追加し、次のコードを追加しました。

MediaPlayer mp;
gridView.setAdapter(new ImageAdapter(this));

gridView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView parent, View v,int position,long id) {
        if (position == 0) {
            mp = MediaPlayer.create(this, R.raw.ok);
            Toast.makeText(getBaseContext(),
                 "Shape Matched",
                 Toast.LENGTH_LONG).show();
            startActivity(new Intent("com.example.TestShapeActivity2"));
        } else {
            mp = MediaPlayer.create(this, R.raw.no);
            Toast.makeText(getBaseContext(),
                    "Please Try Again",
                    Toast.LENGTH_LONG).show();
            //startActivity(new Intent("com.example.TestShapeActivity2"));
        }
    }
});

しかし、作成機能はエラーを出します

The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (new AdapterView.OnItemClickListener(){}, int).

助けてください。ありがとうございます。

4

2 に答える 2

0

遅延が少なく聞こえるので、SoundPool を使用する必要があるかもしれません。最初にSoundPoolを宣言します:

     private SoundPool soundPool;
     private int sound1;

onCreate でサウンドをロードします。最初の数値は、再生のためにキューに入れることができるサウンドの数を設定します。

    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    sound1 = soundPool.load(context, R.raw.msound, 1);

最後に、OnClick で:

soundPool.play(sound1, 1, 1, 1, 0, 1);

この最後の方法は次のようなものです。play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate)これにより、再現する回数などを設定できます。

より詳しい情報

編集:

grid.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View v, int pos, long arg3) {
    soundPool.play(sound1, 1, 1, 1, 0, 1); 
    ...

アイテムのクリックについても同じです:

grid.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long arg3) {
    soundPool.play(soundPulsa, 1, 1, 1, 0, 1);
...
于 2013-06-26T07:39:29.910 に答える