如何将 SoundPool 对象分配给由 Java 中的 Gridview 创建的元素?
Posted
技术标签:
【中文标题】如何将 SoundPool 对象分配给由 Java 中的 Gridview 创建的元素?【英文标题】:How can I assign a SoundPool Object to an element created by Gridview in Java? 【发布时间】:2016-09-28 15:05:23 【问题描述】:我正在开发一个通过 Gridview 显示 151 张图片的 android 应用程序。我有 151 个声音,我想将每个声音分配给一个图像,这样我就可以在图像上播放那个声音。 我正在使用 SoundPool。关于如何做这件事的想法?
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);
);
;
我的 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
;
【问题讨论】:
你的图片在哪里? 在res/drawable文件夹中,音频在res/raw中 你有模型类吗? 【参考方案1】:创建一个模型类,其中包含对您的图像和与之关联的声音的引用。当点击某项时,获取该对象对应的对象,使用引用加载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;
创建项目列表,
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));
将此列表传递给您的适配器。您将修改ImageAdapter
的构造函数。像这样初始化你的适配器,
GridView gridview = (GridView) findViewById(R.id.gridview);
ImageAdapter adapter = new ImageAdapter(this, items);
gridview.setAdapter(adapter);
在你里面 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);
);
【讨论】:
谢谢你,很好的回答,我一回家就试试:) 好的,我遇到了一些麻烦。我从昨晚开始一直在研究它,我有一些问题(请原谅我,我还在学习):首先我得到一个烦人的“无法解决方法 getSoundID()”,我真的不知道怎么做要解决这个问题。其次,我不明白我必须在类 Item 中写 //getters 和 setters 的地方放什么。最后,我在执行加载选项时遇到错误:它告诉人们不能使用“this”,因为类型错误(预期上下文,给定 AdapterView.OnClickListener”...你能帮帮我吗? 哇,这很尴尬,因为我的应用程序在我点击图片后立即崩溃(我不是说这是你的错,显然我不理解你的宝贵帮助)......如果你想在某些方面继续帮助我,我会很高兴,但如果你有其他事情要做,我不会生气。无论如何谢谢:)以上是关于如何将 SoundPool 对象分配给由 Java 中的 Gridview 创建的元素?的主要内容,如果未能解决你的问题,请参考以下文章
Vuetify - 将道具传递给由 router-link 创建的标签