单击GridView项目时如何播放声音?

时间:2015-05-15 15:36:17

标签: android gridview

我已经实现了GridView,并且在点击每个GridView项目时需要不同的声音。到目前为止,我已经实现了此代码,但我对如何实现onItemClick(AdapterView<?> parent, View view, int position, long id)

感到困惑

我的主要活动代码是:

public class MainActivity extends ActionBarActivity {
    MediaPlayer mp = null;
    String c1, c2 = "c2", c3 = "c3", c4 = "c4", c5 = "c5";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
            managerOfSound(c1);
            managerOfSound(c2);
            managerOfSound(c3);
            managerOfSound(c4);
            managerOfSound(c5);
        }
        });
    }

protected void managerOfSound(String theText) {
    if (mp != null) {
        mp.reset();
        mp.release();
    }
    if (position)
        mp = MediaPlayer.create(this, R.raw.c1);
    else if (theText == c2)
        mp = MediaPlayer.create(this, R.raw.c2);
    else if (theText == c3)
        mp = MediaPlayer.create(this, R.raw.c3);
    else if (theText == c4)
        mp = MediaPlayer.create(this, R.raw.c4);
    else
        mp = MediaPlayer.create(this, R.raw.c5);
    mp.start();
   }


}

我的GridView适配器代码是

    public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
    mContext = c;
 }

  public int getCount() {
    return mThumbIds.length;
   }

   public Object getItem(int position) {
      return null;
   }

   public long getItemId(int position) {
    return 0;
  }

   // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setPadding(4, 4, 4, 4);
      } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
   }
   // references to our images
   private Integer[] mThumbIds = {
        R.drawable.cc1, R.drawable.cc2, R.drawable.cc3,
        R.drawable.cc4, R.drawable.cc5, R.drawable.cc6,
        R.drawable.cc7, R.drawable.cc8, R.drawable.cc9,
        R.drawable.cc10, R.drawable.cc11, R.drawable.cc12,
        R.drawable.cc13,
        R.drawable.cc14, R.drawable.cc15, R.drawable.cc16,
        R.drawable.cc17, R.drawable.cc18, R.drawable.cc19,
        R.drawable.cc20,
        R.drawable.cc21, R.drawable.cc22, R.drawable.cc23,
        R.drawable.cc24, R.drawable.cc25, R.drawable.cc26,
        R.drawable.cc27, R.drawable.cc28, R.drawable.cc29,
        R.drawable.cc30;
    };
   }

我的布局代码是

 <?xml version="1.0" encoding="utf-8"?>
 <GridView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/gridview"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:columnWidth="90dp"
 android:numColumns="auto_fit"
 android:verticalSpacing="10dp"
 android:horizontalSpacing="10dp"
 android:stretchMode="columnWidth"
 android:gravity="center"
  />

2 个答案:

答案 0 :(得分:1)

您是否尝试过使用该属性:

android:soundEffectsEnabled="true"

以编程方式编写:

setSoundEffectsEnabled(true);

答案 1 :(得分:0)

您使用此代码:

   public void onItemClick(AdapterView parent, View view,
     int position, long id) {
//if the user clicks the specific photo in gridview this sounds will be played
//notice the position numbers! 
switch (position) {
         case 0:
           mPlayer.start();  // This will make  sound
           Toast.makeText(MainActivity.this, "Cat sound playing " ,
       Toast.LENGTH_SHORT).show();

           break;
         case 1:
           mPlayer2.start(); 
           Toast.makeText(MainActivity.this, "Cow sound playing " ,
       Toast.LENGTH_SHORT).show();
           // This will make different sound
             break;
         case 2:
           mPlayer3.start(); // This will make different sound

           Toast.makeText(MainActivity.this, "Dog sound playing " ,
       Toast.LENGTH_SHORT).show();

           break;

         case 3:
           mPlayer4.start(); // This will make different sound
           Toast.makeText(MainActivity.this, "Monkey sound playing " ,
       Toast.LENGTH_SHORT).show();

           break;

         case 4:
           mPlayer5.start(); // This will make different sound

           Toast.makeText(MainActivity.this, "Frog sound playing " ,
       Toast.LENGTH_SHORT).show();


           break;
               }

   }
       });

}

或参考此link

投票!