在列表项目上单击播放特定声音

时间:2014-11-07 15:20:32

标签: android listview audio resources

我有这个应用程序,我有一个列表视图,我想在点击一个项目时发出声音。我尝试了很多东西,但似乎没有一个能解决我的问题。

这是一个包含单词,翻译和播放按钮的列表视图,当我点击按钮时我想播放特定单词的声音。这里我有我的适配器和使用的类。如果有人知道如何回答这个问题,我会很高兴;)

CLASS:

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

        }
    }

ADAPTER:

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;
    }
}

3 个答案:

答案 0 :(得分:0)

您可以将所有声音以mp3或wave格式放入项目目录中的原始文件夹中,然后使用以下代码。

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;
                }
    }

希望它会有所帮助。

答案 1 :(得分:0)

我认为这就是你想要的,R.raw.example_sound可以放在raw下的地图(values)中,如果它不存在则自己创建

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.example_sound);
mediaPlayer.start()

答案 2 :(得分:0)

我认为你必须创建自己的'AccessibilityService'并使用'TextToSpeech'来提供

用户点击的语音反馈。

相关问题