0

I'm trying to play .mp3 file from ListView I have tried following but its not working

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();
    }
}
}

can anyone tell me how to do this and what i'm missing ?

EDIT I have tried with Uri passing as

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

but the file is not playing and the Error Log is as follows

QCMediaPlayer mediaplayer NOT present

can anyone tell me whats going wrong and what to do?

Ravi Patil
  • 127
  • 2
  • 16

2 Answers2

1

Try this in playSong method

Uri myUri = Uri.parse("file:///sdcard/mp3/example.mp3");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(getApplicationContext(), myUri);
mp.prepare();
mp.start();
C Van
  • 81
  • 1
  • 5
0

It looks like the log

QCMediaPlayer mediaplayer NOT present

is not related.

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

I would add a progressListener to see if it's playing but it's not outputting for some reason.

LukeWaggoner
  • 8,869
  • 1
  • 29
  • 28
  • what is the reason behind that and the link you have given also don't have any answer – Ravi Patil Feb 27 '18 at 22:43
  • There are a number of reasons why the media player wouldn't play music. I'm wondering if it actually is playing (or thinks it is), but it's just not outputting. I would also add a log statement for your file along the lines of `file.exists()` to make sure that you don't have a path error. – LukeWaggoner Feb 28 '18 at 20:35