I am developing an application that contains a RecyclerView with some audio players. The app will download the .3gp files if they have not been downloaded.
When I click the playAudio button, the audio is not being played.
Here is my adapter code:
else if( item.getTipo().equals(AUDIO)){
MediaPlayer mediaPlayer = new MediaPlayer();
File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + "Audios");
File file = new File(folder.getPath() + File.separator
+ item.getNomeArquivo());
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference httpsReference = storage.getReferenceFromUrl( item.getAudio());
File localFile = null;
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
localFile = new File(folder.getPath() + File.separator
+ item.getNomeArquivo());
File finalLocalFile = localFile;
httpsReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(context,"Baixado",Toast.LENGTH_SHORT);
holder.progressAudio.setVisibility(View.GONE);
holder.playAudio.setVisibility(View.VISIBLE);
holder.playAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mediaPlayer != null){
String path = Environment.getExternalStorageDirectory() +
File.separator + "Audios" + File.separator
+ item.getNomeArquivo();
Log.i("PATHA",path);
mediaPlayer.create(context,Uri.parse(path));
mediaPlayer.setVolume(50,50);
mediaPlayer.start();
}
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Toast.makeText(context,"Erro ao fazer download",Toast.LENGTH_SHORT);
}
});
} else {
Toast.makeText(context, "Erro ao criar arquivo", Toast.LENGTH_SHORT).show();
}
}
How can I fix this?
Try replacing:
mediaPlayer.create(context,Uri.parse(path));
with:
mediaPlayer.create(context, Uri.fromFile(new File(path)));