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

标签 java android audio android-arrayadapter

我正在尝试从 ListView 播放 .mp3 文件 我已尝试以下操作,但它不起作用

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 mediaplayer NOT present

谁能告诉我出了什么问题以及该怎么办?

最佳答案

在 playSong 方法中尝试一下

Uri myUri = Uri.parse("file:///sdcard/mp3/example.mp3");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(getApplicationContext(), myUri);
mp.prepare();
mp.start();

关于java - 如何解决 QCMediaPlayer 媒体播放器不存在错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49017705/

相关文章:

针对 PostgreSQL 8.4.6 的 java.sql.Connection 测试

java - 错误: No resource found that matches the given name (at 'srcCompat' with value '@drawable-hdpi/ic_launcher' ) in eclispe

java - 在哪里放置相机请求权限?

android - 如何发送彩信而不提示通过 gmail、whatsapp 或其他方式发送?

android - Mediaplayer没有声音

javascript - 从路径或URL初始化音频WAV文件对象

java - objectinputstream.readobject() 行 : not available [local variables unavailable]

java.lang.IllegalStateException : ScrollView can host only one direct child

android - 为什么在创建 Intent 实例时传递 'this'?

c# - 如何在Xamarin中使用MediaPlayer获得跟踪的持续时间