I want to play audio files which are stored on internal storage of my application from a user device: here below are the full code of extracting and showing list of those audio files=
//variables of retrieving audio file from internal storage
ListView listView_phone_calls;
ArrayList<File> phone_call_list;
ArrayAdapter<File> adapter;
//variables of playing an audio file
MediaPlayer player;
listView_phone_calls=(ListView)findViewById(R.id.listView_phone_calls);
phone_call_list=new ArrayList<>();
File[] file=getApplicationContext().getFilesDir().listFiles();
for (int i=0;i<file.length;i++){
phone_call_list.add(file[i]);
}
adapter=new ArrayAdapter<>(PhoneCall_Player.this,android.R.layout.simple_list_item_activated_1,phone_call_list);
listView_phone_calls.setAdapter(adapter);
listView_phone_calls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//This is what should happen when a user clicks on a one item
player=new MediaPlayer();
File call_rec=(File) listView_phone_calls.getItemAtPosition(position);
try {
player.setDataSource(String.valueOf(call_rec));
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(PhoneCall_Player.this,"Sorry! File not found! "+e.getMessage(),Toast.LENGTH_SHORT).show();
}catch (IllegalArgumentException e){
e.printStackTrace();
Toast.makeText(PhoneCall_Player.this,"Sorry! Something is wrong!, "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(PhoneCall_Player.this,"Player couldn't be prepared to play! "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
player.release();
player=null;
}
});
}
});
Whenever I click on an item, the toast of player couldn't be prepared to play keeps appearing; I think there is no problem with setDataSource(); so what is wrong with my codes. Is there any error that I am putting that I don't see? Or I am not calling MediaPlayer well?