如何解决QCMediaPlayer媒体播放器不存在错误?

时间:2018-02-27 20:48:33

标签: java android audio android-arrayadapter

我正在尝试播放来自.mp3 ListView的{​​{1}}文件我尝试过但是它无效

public static class FirstFragment extends Fragment {
    private MediaPlayer mp;
    View myView;
   private  String[] theNamesOfFiles;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        mp=new MediaPlayer();
        myView= inflater.inflate(R.layout.first_layout,container,false);

        File dir = new File(Environment.getExternalStorageDirectory()+File.separator+"Ringtones");
            File[] filelist = dir.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return !pathname.isHidden();
                }
            });
            theNamesOfFiles = new String[filelist.length];
            for (int i = 0; i < theNamesOfFiles.length; i++) {
                theNamesOfFiles[i] = filelist[i].getName();
                int pos = theNamesOfFiles[i].lastIndexOf(".");
                if (pos > 0) {
                    theNamesOfFiles[i] = theNamesOfFiles[i].substring(0, pos);
                }
            }

        ListView listview=(ListView) myView.findViewById(R.id.RecordingList);
        ArrayAdapter<String> listviewAdapter= new ArrayAdapter<String>(
                getActivity(),android.R.layout.simple_list_item_1,theNamesOfFiles
        );
        listview.setAdapter(listviewAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                playSong(position);
            }
        });
        return myView;
    }
    public void playSong(int songIndex)
    {
        //Play Songs
        mp.reset();
        mp=MediaPlayer.create(getActivity().getApplicationContext(),theNamesOfFiles[songIndex]);
        mp.start();
    }
}
}

谁能告诉我如何做到这一点以及我缺少什么?

修改 我试过Uri传递为

 Uri myUri = Uri.parse(theNamesOfFiles[position]);

但文件未播放且错误日志如下

  

QCMediaPlayer媒体播放器不存在

谁能告诉我哪里出错了怎么办?

2 个答案:

答案 0 :(得分:1)

在playSong方法中试试这个

TreeMap

答案 1 :(得分:0)

看起来像日志

  

QCMediaPlayer媒体播放器不存在

无关。

Why MediaPlayer throws NOT present error when creating instance of it?

我会添加一个progressListener来查看它是否正在播放但是由于某种原因它没有输出。