简体   繁体   English

在列表项上播放特定的声音,请单击

[英]Play specific sound on list item click

I have this app where I have a listview and I would like to play a sound when an item is clicked. 我在此应用中有一个列表视图,我想在单击某个项目时播放声音。 I tried a lot of things but none of them seem to solve my problem. 我尝试了很多事情,但似乎都无法解决我的问题。

It's a listview with words, it's translation and a play button, when I click the button I want to play the sound of the specific word. 这是一个包含单词的列表视图,它是翻译和一个播放按钮,当我单击该按钮时,我想播放特定单词的声音。 Here I have my adapter and the class used. 在这里,我有我的适配器和使用的类。 If someone knows how to answer this question I would be happy ;) 如果有人知道如何回答这个问题,我会很高兴;)

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

You can put all your sounds in mp3 or wave format into the raw folder in the project directory, and then use the following code. 您可以将所有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;
                }
    }

Hope it'll help. 希望能对您有所帮助。

I think this is what you want, the R.raw.example_sound can be placed in map( raw ) under values , if it does not exist create it yourself 我想这就是您想要的,可以将R.raw.example_sound放在map( rawvalues下,如果不存在,请自行创建

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

I think you have to create your own 'AccessibilityService 'and use 'TextToSpeech' to provide 我认为您必须创建自己的“ AccessibilityService”并使用“ TextToSpeech”来提供

spoken feedback for the users click. 用户点击的语音反馈。

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

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