简体   繁体   English

如何将SoundPool对象分配给Java中Gridview创建的元素?

[英]How can I assign a SoundPool Object to an element created by Gridview in Java?

I'm working on an Android App that displays 151 pictures via Gridview. 我正在开发一个通过Gridview显示151张图片的Android应用。 I have 151 sounds and I want to assign every sound to a single image, so I can play that sound tapping on an Image. 我有151种声音,我想将每种声音分配给单个图像,因此可以在图像上轻按该声音。 I'm using SoundPool. 我正在使用SoundPool。 Ideas about how can I do this thing? 关于如何执行此操作的想法?

package com.example.thefe.newsmartkedex;

import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import static com.example.thefe.newsmartkedex.R.raw.pkmn1;

public class MainActivity extends AppCompatActivity {

    int pkmn1, pkmn2, pkmn3;
    SoundPool mySoundPool = new SoundPool (1, AudioManager.STREAM_MUSIC, 0);


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pkmn1 = mySoundPool.load(this, R.raw.pkmn1, 1);
        pkmn2 = mySoundPool.load(this, R.raw.pkmn2, 1);
        pkmn3 = mySoundPool.load(this, R.raw.pkmn3, 1);


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

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                 int position, long id) {
                position += 1; //forget about this
                Toast.makeText(MainActivity.this, "" + position,
                        Toast.LENGTH_SHORT).show();
                mySoundPool.play(/*???*/,1,1,1,0,1);
            }
        });
    };
}

My ImageAdapter Class works like this: 我的ImageAdapter类的工作方式如下:

package com.example.thefe.newsmartkedex;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import java.util.ArrayList;

/**
 * Created by TheFe on 27/09/2016.
 */

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c, ArrayList list) {
        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_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.pkmn1, R.drawable.pkmn2,
            R.drawable.pkmn3, R.drawable.pkmn4,
            R.drawable.pkmn5, R.drawable.pkmn6,
            R.drawable.pkmn7, R.drawable.pkmn8,
            R.drawable.pkmn9, R.drawable.pkmn10,
            R.drawable.pkmn11, R.drawable.pkmn12,
            R.drawable.pkmn13, R.drawable.pkmn14,
            R.drawable.pkmn15, R.drawable.pkmn16,
            R.drawable.pkmn17, R.drawable.pkmn18,
            R.drawable.pkmn19, R.drawable.pkmn20,
            R.drawable.pkmn21, R.drawable.pkmn22,
            R.drawable.pkmn23, R.drawable.pkmn24,
            R.drawable.pkmn25, R.drawable.pkmn26,
            R.drawable.pkmn27, R.drawable.pkmn28,
            R.drawable.pkmn29, R.drawable.pkmn30,
            R.drawable.pkmn31, R.drawable.pkmn32,
            R.drawable.pkmn33, R.drawable.pkmn34,
            R.drawable.pkmn35, R.drawable.pkmn36,
            R.drawable.pkmn37, R.drawable.pkmn38,
            R.drawable.pkmn39, R.drawable.pkmn40,
            R.drawable.pkmn41, R.drawable.pkmn42,
            R.drawable.pkmn43, R.drawable.pkmn44,
            R.drawable.pkmn45, R.drawable.pkmn46,
            R.drawable.pkmn47, R.drawable.pkmn48,
            R.drawable.pkmn49, R.drawable.pkmn50,
            R.drawable.pkmn51, R.drawable.pkmn52,
            R.drawable.pkmn53, R.drawable.pkmn54,
            R.drawable.pkmn55, R.drawable.pkmn56,
            R.drawable.pkmn57, R.drawable.pkmn58,
            R.drawable.pkmn59, R.drawable.pkmn60,
            R.drawable.pkmn61, R.drawable.pkmn62,
            R.drawable.pkmn63, R.drawable.pkmn64,
            R.drawable.pkmn65, R.drawable.pkmn66,
            R.drawable.pkmn67, R.drawable.pkmn68,
            R.drawable.pkmn69, R.drawable.pkmn70,
            R.drawable.pkmn71, R.drawable.pkmn72,
            R.drawable.pkmn73, R.drawable.pkmn74,
            R.drawable.pkmn75, R.drawable.pkmn76,
            R.drawable.pkmn77, R.drawable.pkmn78,
            R.drawable.pkmn79, R.drawable.pkmn80,
            R.drawable.pkmn81, R.drawable.pkmn82,
            R.drawable.pkmn83, R.drawable.pkmn84,
            R.drawable.pkmn85, R.drawable.pkmn86,
            R.drawable.pkmn87, R.drawable.pkmn88,
            R.drawable.pkmn89, R.drawable.pkmn90,
            R.drawable.pkmn91, R.drawable.pkmn92,
            R.drawable.pkmn93, R.drawable.pkmn94,
            R.drawable.pkmn95, R.drawable.pkmn96,
            R.drawable.pkmn97, R.drawable.pkmn98,
            R.drawable.pkmn99, R.drawable.pkmn100,
            R.drawable.pkmn101, R.drawable.pkmn102,
            R.drawable.pkmn103, R.drawable.pkmn104,
            R.drawable.pkmn105, R.drawable.pkmn106,
            R.drawable.pkmn107, R.drawable.pkmn108,
            R.drawable.pkmn109, R.drawable.pkmn110,
            R.drawable.pkmn111, R.drawable.pkmn112,
            R.drawable.pkmn113, R.drawable.pkmn114,
            R.drawable.pkmn115, R.drawable.pkmn116,
            R.drawable.pkmn117, R.drawable.pkmn118,
            R.drawable.pkmn119, R.drawable.pkmn120,
            R.drawable.pkmn121, R.drawable.pkmn122,
            R.drawable.pkmn123, R.drawable.pkmn124,
            R.drawable.pkmn125, R.drawable.pkmn126,
            R.drawable.pkmn127, R.drawable.pkmn128,
            R.drawable.pkmn129, R.drawable.pkmn130,
            R.drawable.pkmn131, R.drawable.pkmn132,
            R.drawable.pkmn133, R.drawable.pkmn134,
            R.drawable.pkmn135, R.drawable.pkmn136,
            R.drawable.pkmn137, R.drawable.pkmn138,
            R.drawable.pkmn139, R.drawable.pkmn140,
            R.drawable.pkmn141, R.drawable.pkmn142,
            R.drawable.pkmn143, R.drawable.pkmn144,
            R.drawable.pkmn145, R.drawable.pkmn146,
            R.drawable.pkmn147, R.drawable.pkmn148,
            R.drawable.pkmn149, R.drawable.pkmn150,
            R.drawable.pkmn151
    };
}

Create a model class that holds a reference to your image and the sound associated with it. 创建一个模型类,其中包含对图像和与之关联的声音的引用。 When an item is clicked, get the object corresponding to the object, load the SoundPool object using the reference and play it. 单击一个项目时,获取与该对象相对应的对象,使用引用加载SoundPool对象并播放它。

public class Item {

    int imageId;
    int soundID;

    public Item(int imageId, int soundID){
        this.imageId = imageId;
        this.soundID = soundID;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }

    public int getSoundID() {
        return soundID;
    }

    public void setSoundID(int soundID) {
        this.soundID = soundID;
    }
}

Create a List of Items, 创建项目列表,

List<Item> items = new ArrayList<Items>();
for(int i = 0; i < 10; i++) {
    items.add(new Item(R.drawable.your_image_id, R.raw.your_sound_id));
}

Pass this list to your adapter. 将此列表传递到您的适配器。 You will to modify the constructor of ImageAdapter . 您将修改ImageAdapter的构造ImageAdapter Initialize your adapter like this, 这样初始化您的适配器,

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

Inside you onItemClick, 在您的onItemClick内部,

gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        // your code

        Item selectedItem = items.get(position);
        int soundID = selectedItem.getSoundID();
        pkmn1 = mySoundPool.load(MainActivity.this, soundID, 1);
        mySoundPool.play(pkmn1, 1, 1, 1, 0, 1);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM