简体   繁体   English

单击时如何播放Listview项目的特定声音

[英]How to play a specific sound for a Listview item when it is clicked

I am creating an android dictionary app with sounds... I have listview , when an item is selected, a new activity open, inside the new activity contains 4 textviews and an image button, the textviews function perfectly but the image button was not. 我正在创建一个带有声音的android词典应用...我有listview ,当选择一个项目时,一个新的活动打开,在新活动内包含4个textviews和一个图像按钮, textviews功能完美,但image按钮却没有。 The audio files are placed in raw folder. 音频文件放置在原始文件夹中。 How can I put the specific sounds of an item that was clicked? 如何放置被单击的项目的特定声音? Here's the code: 这是代码:

MainActivityJava MainActivityJava

public class MainActivity extends AppCompatActivity {

ListView lv;
SearchView sv;


String[] tagalog= new String[] {"alaala (png.)","araw (png.)","baliw (png.)","basura (png.)",
        "kaibigan (png.)","kakatuwa (pu.)", "kasunduan (png.)","dambuhala (png.)",
        "dulo (png.)","gawin (pd.)","guni-guni (png.)","hagdan (png.)","hintay (pd.)",
        "idlip (png.)","maganda (pu.)","masarap (pu.)", "matalino (pu.)"};

int[] sounds= new int[]{R.raw.alaala,
                R.raw.araw,
                R.raw.baliw,
                R.raw.basura,
                R.raw.kaibigan,
                R.raw.kakatuwa,
                R.raw.kasunduan,
                };


ArrayAdapter<String> adapter;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    lv = (ListView) findViewById(R.id.listView1);
    sv = (SearchView) findViewById(R.id.searchView1);

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,tagalog);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String tagword =tagalog[position];

            String[] definition = getResources().getStringArray(R.array.definition);
            final String definitionlabel = definition[position];

            String[] cuyuno = getResources().getStringArray(R.array.cuyuno);
            final String cuyunodefinition = cuyuno[position];

            String[] english = getResources().getStringArray(R.array.english);
            final String englishdefinition = english[position];


            Intent intent = new Intent(getApplicationContext(), DefinitionActivity.class);
            intent.putExtra("tagword", tagword);
            intent.putExtra("definitionlabel", definitionlabel);
            intent.putExtra("cuyunodefinition",cuyunodefinition);
            intent.putExtra("englishdefinition", englishdefinition);



            startActivity(intent);

        }
    });


    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String text) {

            adapter.getFilter().filter(text);
            return false;
        }



    });

}

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

} }

DefinitionActivity.java DefinitionActivity.java

public class DefinitionActivity extends AppCompatActivity {

MediaPlayer mp;

String tagalogword;
String worddefinition;
String cuyunoword;
String englishword;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_definition);

    TextView wordtv = (TextView) findViewById(R.id.wordtv);
    TextView definitiontv = (TextView) findViewById(R.id.definitiontv);
    TextView cuyunotv = (TextView) findViewById(R.id.cuyunotv);
    TextView englishtv = (TextView) findViewById(R.id.englishtv);
    ImageButton playbtn = (ImageButton) findViewById(R.id.playbtn);


    final Bundle extras = getIntent().getExtras();

    if (extras != null) {

        tagalogword = extras.getString("tagword");
        wordtv.setText(tagalogword);

        worddefinition = extras.getString("definitionlabel");
        definitiontv.setText(worddefinition);

        cuyunoword = extras.getString("cuyunodefinition");
        cuyunotv.setText(cuyunoword);

        englishword = extras.getString("englishdefinition");
        englishtv.setText(englishword);

    }

    playbtn.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {

        }
    });


}

您可以在意向附加中传递原始id并在meadiaPlayer上播放

What you want to accomplish is pretty simple. 您想要完成的工作非常简单。

you can ofcourse pass the id. 您当然可以传递ID。

But I created this method for your case you can paste it in your activity or class and make a call to it. 但是我为您创建了此方法,您可以将其粘贴到您的活动或类中并对其进行调用。 In my case, I put this method in a class that holds all the common functions, methods, strings, etc. The choice is yours : 就我而言,我将此方法放在包含所有常用功能,方法,字符串等的类中。选择是您自己的:

public static void playDisSound(Context c, int soundID){
                //Play short tune
                MediaPlayer mediaPlayer = MediaPlayer.create(c, soundID);
                mediaPlayer.setOnCompletionListener( new OnCompletionListener(){
                    @Override
                    public void onCompletion( MediaPlayer mp){
                        mp.release();
                    }
                });
                mediaPlayer.start();
            }

And this is how to use it in your case : 这是如何在您的情况下使用它:

Example I want to play an audio track from : 示例我要播放来自的音轨:

int[] sounds= new int[]{R.raw.alaala,
                R.raw.araw,
                R.raw.baliw,
                R.raw.basura,
                R.raw.kaibigan,
                R.raw.kakatuwa,
                R.raw.kasunduan,
                };

So I just do : 所以我就这样做:

//TODO ~ pls. remember to define context inside "onCreate" as 

//call this before "onCreate"
Context context;

//And do this inside "onCreate"   :
context = getApplicationContext();
OR
context = MainActivity.this;

//Then here comes the solution, just make a call to the playDisSound method with the id , in this case the "sounds[postion_referencer_i]" //然后出现解决方案,只需使用id调用playDisSound方法,在本例中为“ sounds [postion_referencer_i]”

playDisSound(context, sounds[postion_referencer_i]);

//And now on the question of what your "position_referencer_i" would be .... it also depends on how you intend to pass the id. //现在,您的“ position_referencer_i”将是...的问题也取决于您打算如何传递ID。

Are your going to make a match between the position picked and the position of the sound. 您要在拾取的位置和声音的位置之间进行匹配吗? It depends on you. 这取决于你。 But I would have created a set of integers to signify which try I want to play and do a matching simple calculation between the position picked for the item clicked to arrive at the position_referencer_id. 但是我会创建一组整数来表示我想玩哪种尝试,并在为单击而到达position_referencer_id的项目所选择的位置之间进行匹配的简单计算。

//But simply : note that in your array if I want to play for example "R.raw.baliw" I would just call : //但是很简单:请注意,在您的数组中,如果我想播放例如“ R.raw.baliw”,我只会调用:

playDisSound(context, R.raw.baliw);

I hope this works perfectly for you. 我希望这对您来说很完美。 So if I elaborated too much. 因此,如果我详细说明。 Do let me know if you may need to stream the sound so I would just paste/send you a very cool method I have been using here in an app am working. 如果您可能需要流式传输声音,请告诉我,以便我粘贴/发送给您一个我在应用中一直在使用的非常酷的方法。

//FINALLY PLS. //最后PLS。 Remember this : this method would play the sound alright but it wont hesitate to play the sound all over again if you repeat the process. 请记住:此方法可以正常播放声音,但是如果您重复此过程,它将毫不犹豫地重新播放声音。 So do remember to check if the sound did play and finished before allowing the user to repeat, if not it could lead to repeated or kind of two speakers playing from the same song but at different time. 因此,请务必记住在允许用户重复播放之前检查声音是否播放和结束,否则可能会导致重复播放或两种扬声器从同一首歌曲但在不同时间播放。 (And the user may start to think that there is problem with the app. Pls. be very logical and sensitive in using this method) (并且用户可能开始认为该应用存在问题。请在使用此方法时非常合逻辑且敏感)

In solving that, you can disable the button or the UI element that initiates the sound playing until the sound has finished playing, by way of monitoring duration of the track (which I am sure you should know and inculcate into your logic or by simply listening if sound is already playing) 为了解决这个问题,您可以通过监视音轨的持续时间(我确定您应该了解并灌输您的逻辑,或者通过简单地收听)来禁用启动声音播放的按钮或UI元素,直到声音播放完毕为止。如果声音已经在播放)

All the best. 祝一切顺利。 Era. 时代。 :) :)

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

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