简体   繁体   English

如何在Android中动态播放音频文件

[英]how to play a audio file dynamically in android

I have listview with dynamic list of audio files which is stored in a particular folder in sd card when user click a particular file i will get postion in int in onItemClick method i need to create alert dialog box with two buttons first is to play that particular file of that particular position and second is delete that file. 我有带有音频文件动态列表的listview,当用户单击特定文件时,该文件存储在sd卡中的特定文件夹中,我将在onItemClick方法中获得int位置,我需要创建带有两个按钮的警报对话框,首先是播放该特定对话框该特定位置的文件,其次是删除该文件。 how to do this? 这个怎么做?

register your list with the onItemClickListener and inside that method populate a Dialog that have 2 buttons 使用onItemClickListener注册您的列表,并在该方法内部填充一个具有2个按钮的Dialog

here is some code : 这是一些代码:

mylistview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long id) {

            String file_path_in_the_sd_card = YOURADAPTER.getFilePathAtPosition(position);

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Option")
                   .setCancelable(false)
                   .setPositiveButton("Play", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           //start the activity the will play the song and provide the path to it 

                       }
                   })
                   .setNegativeButton("Delete", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           //make a code that will delete the file maybe like this 
                           File file = new File(file_path_in_sd_card);
                           file.delete();

                           //remove it aslo from your adapter 

                           myadpter.removeFile(poistion);
                           myadpater.notifyDataSetChange();

                            dialog.cancel();
                       }
                   });
            AlertDialog alert = builder.create();




}

this code to help you think how to do that job it not a complete example . 这段代码可以帮助您思考如何完成该工作,而不是一个完整的示例。

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

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