我在此应用中有一个列表 View ,我想在单击某个项目时播放声音。我尝试了很多事情,但似乎都无法解决我的问题。
这是一个包含单词的列表 View ,它是翻译和一个播放按钮,当我单击该按钮时,我想播放特定单词的声音。在这里,我有我的适配器和使用的类。如果有人知道如何回答这个问题,我会很高兴;)
类:
public class VB_Itens extends ListActivity {
private VB_Itens_Adapter adapter;
private int wordsArrayId, translationsArrayId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int position = getIntent().getIntExtra("position", 0);
// Here i populate the list with the words and it's translations from arrays resource
switch (position) {
case 0:
wordsArrayId = R.array.VB_airport_words;
translationsArrayId = R.array.VB_airport_translations;
break;
case 1:
wordsArrayId = R.array.VB_hotel_words;
translationsArrayId = R.array.VB_hotel_translations;
break;
case 2:
wordsArrayId = R.array.VB_hospital_words;
translationsArrayId = R.array.VB_hospital_translations;
break;
case 3:
wordsArrayId = R.array.VB_restaurant_words;
translationsArrayId = R.array.VB_restaurant_translations;
break;
}
adapter = new VB_Itens_Adapter(this, wordsArrayId, translationsArrayId);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// I want to play a specific sound and the item is clicked
// I don't know how to take these sounds
}
}
适配器:
public class VB_Itens_Adapter extends BaseAdapter {
private List<Palavra> listaPalavras = new ArrayList<Palavra>();
private LayoutInflater inflater;
public VB_Itens_Adapter(Context context, int resPalavra, int resTraducao) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
String palavras[] = context.getResources().getStringArray(resPalavra);
String traducoes[] = context.getResources().getStringArray(resTraducao);
for (int i = 0; i < palavras.length; i++) {
Palavra palavra = new Palavra(palavras[i], traducoes[i]);
listaPalavras.add(palavra);
}
}
@Override
public int getCount() {
return listaPalavras.size();
}
@Override
public Object getItem(int position) {
return listaPalavras.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = inflater.inflate(R.layout.vb_itens, null);
Palavra palavra = listaPalavras.get(position);
TextView palavraView = (TextView) view.findViewById(R.id.palavra);
TextView traducaoView = (TextView) view.findViewById(R.id.traducao);
palavraView.setText(palavra.getPalavra());
traducaoView.setText(palavra.getTradução());
return view;
}
}
最佳答案
您可以将所有mp3或wave格式的声音放入项目目录中的raw文件夹中,然后使用以下代码。
final MediaPlayer mp;
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0:
mp = MediaPlayer.create(this, R.raw.sound_of_a);
mp.start();
break;
case 1:
mp = MediaPlayer.create(this, R.raw.sound_of_b);
mp.start();
break;
case 2:
mp = MediaPlayer.create(this, R.raw.sound_of_c);
mp.start();
break;
case 3:
mp = MediaPlayer.create(this, R.raw.sound_of_d);
mp.start();
break;
}
}
希望能对您有所帮助。
关于android - 在列表项上播放特定的声音,请单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26804401/