我想从Listview播放选定的mp3文件

时间:2016-06-09 07:20:32

标签: android android-mediaplayer baseadapter

我只想从列表中播放选定的mp3文件。确认onItemClick的位置,但列表视图没有响应。

从SDCard获取mp3文件。 请帮我 提前谢谢。

我的片段

ListView lv_recordersList;
 private ListViewAdapter adapter;
 ProgressDialog mProgressDialog;
 private List<String> myList;
 File file;


        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub

            View v = inflater.inflate(R.layout.frag_recorders_list, null);
            lv_recordersList = (ListView) v.findViewById(R.id.lv_recordersList);

            myList = new ArrayList<String>();


            new DownloadJSON().execute();

        lv_recordersList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
             String value = lv_recordersList.getItemAtPosition(position).toString();

             System.out.println("####### Value OnItemClick : " +value);

             try {
             mMediaPlayer.setDataSource(value);
                mMediaPlayer.prepare();
                mMediaPlayer.start();
             } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    });

            return v;
        }



    public class DownloadJSON extends AsyncTask<Void, Void, Void>{

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            mProgressDialog = new ProgressDialog(getActivity());
            //mProgressDialog.setTitle("PlugLeads");
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();

        }


        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            File directory = Environment.getExternalStorageDirectory();
            file = new File(directory + "/Plugleads");
            File list[] = file.listFiles();

            for (int i = 0; i < list.length; i++) {
                // if(checkExtension( list[i].getName())
                if (checkExtension(list[i].getName())) {
                    myList.add(list[i].getName());
                }
            }


            return null;
        }




        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            //listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(getActivity(), myList);
            // Set the adapter to the ListView
            lv_recordersList.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }



        private boolean checkExtension(String fileName) {
            String ext = getFileExtension(fileName);
            if (ext == null)
                return false;
            try {
                if (SupportedFileFormat.valueOf(ext.toUpperCase()) != null) {
                    return true;
                }
            } catch (IllegalArgumentException e) {
                return false;
            }
            return false;
        }

        public String getFileExtension(String fileName) {
            int i = fileName.lastIndexOf('.');
            if (i > 0) {
                return fileName.substring(i + 1);
            } else
                return null;
        }


    }

我的自定义BaseAdapter getView():

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    ImageView img_recorders_listitem;
    TextView tv_recorders_listitem;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    View itemView = inflater.inflate(R.layout.frag_recorders_listitem, parent, false);

    img_recorders_listitem = (ImageView) itemView.findViewById(R.id.img_recorders_listitem);
    tv_recorders_listitem = (TextView) itemView.findViewById(R.id.tv_recorders_listitem);


    //resultp = data.get(position);
    System.out.println("$$$$$$$ ListP : " +myListp);

    pos = myListp.get(position);

    System.out.println("$$$$$$$ Pos : " +pos);


    tv_recorders_listitem.setText(pos);


    mMediaPlayer = new MediaPlayer();


    return itemView;
}

我的Logcat

06-09 13:05:54.787: I/System.out(16669): ####### Value OnItemClick : gshsh_07-06-2016_12_11_17.mp3
06-09 13:05:54.803: W/System.err(16669): java.io.IOException: setDataSource failed.
06-09 13:05:54.804: W/System.err(16669):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1086)
06-09 13:05:54.808: W/System.err(16669):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1032)
06-09 13:05:54.809: W/System.err(16669):    at com.plugleads.feedback.record.Frag_RecordersList$1.onItemClick(Frag_RecordersList.java:69)
06-09 13:05:54.809: W/System.err(16669):    at android.widget.AdapterView.performItemClick(AdapterView.java:305)
06-09 13:05:54.809: W/System.err(16669):    at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
06-09 13:05:54.809: W/System.err(16669):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
06-09 13:05:54.809: W/System.err(16669):    at android.widget.AbsListView$3.run(AbsListView.java:3860)
06-09 13:05:54.809: W/System.err(16669):    at android.os.Handler.handleCallback(Handler.java:739)
06-09 13:05:54.814: W/System.err(16669):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-09 13:05:54.814: W/System.err(16669):    at android.os.Looper.loop(Looper.java:135)
06-09 13:05:54.814: W/System.err(16669):    at android.app.ActivityThread.main(ActivityThread.java:5343)
06-09 13:05:54.814: W/System.err(16669):    at java.lang.reflect.Method.invoke(Native Method)
06-09 13:05:54.814: W/System.err(16669):    at java.lang.reflect.Method.invoke(Method.java:372)
06-09 13:05:54.814: W/System.err(16669):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
06-09 13:05:54.814: W/System.err(16669):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

请帮帮我。谢谢。

1 个答案:

答案 0 :(得分:2)

与日志一样:

IOException: setDataSource failed.

因为gshsh_07-06-2016_12_11_17.mp3不是MediaPlayer的有效文件路径。

使用完整路径,包括当前可用文件的目录名称。像:

File directory = Environment.getExternalStorageDirectory();
file = new File(directory + "/Plugleads/"+ value);
FileInputStream fileInputStream = new FileInputStream(file);
mMediaPlayer.setDataSource(fileInputStream.getFD());